Skip to main content
Revise title, question; format code indent; update tags
Source Link
Yong Shun
  • 54.5k
  • 6
  • 38
  • 66

MongoDB - Count unread messages in MongoDb

I try to count unread messages for a user. On

On my model i, I have a property, LastMessageDateLastMessageDate that contains the date on the last created message in the group chat. I have also a Members propeerty Members property (list) that contains the members in the group chat. Each member has a UserIdthe UserId and LastReadDate propertyLastReadDate properties. The LastReadDateLastReadDate is updated when the user writewrites a new message in the group chat or when the user loads messages from the group chat.

Now iI want to count the number of chats where a specific user has unreadedunread messages (The messages isare stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);
var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But iI receive the following error:

The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should iI do? Is there a better solution?

Count unread messages in MongoDb

I try to count unread messages for a user. On my model i have a property, LastMessageDate that contains the date on the last created message in the group chat. I have also a Members propeerty (list) that contains the members in the group chat. Each member has a UserId and LastReadDate property. The LastReadDate is updated when the user write a new message in the group chat or when the user loads messages from the group chat.

Now i want to count the number of chats where a specific user has unreaded messages (The messages is stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But i receive the following error:

The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should i do? Is there a better solution?

MongoDB - Count unread messages

I try to count unread messages for a user.

On my model, I have a property, LastMessageDate that contains the date on the last created message in the group chat. I have also a Members property (list) that contains the members in the group chat. Each member has the UserId and LastReadDate properties. The LastReadDate is updated when the user writes a new message in the group chat or when the user loads messages from the group chat.

Now I want to count the number of chats where a specific user has unread messages (The messages are stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But I receive the following error:

The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should I do? Is there a better solution?

Formatted code
Source Link
Markus
  • 22.7k
  • 4
  • 34
  • 61

I try to count unread messages for a user. On my model i have a property, LastMessageDate that contains the date on the last created message in the group chat. I have also a Members propeerty (list) that contains the members in the group chat. Each member has a UserId and LastReadDate property. The LastReadDate is updated when the user write a new message in the group chat or when the user loads messages from the group chat.

Now i want to count the number of chats where a specific user has unreaded messages (The messages is stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But i recivereceive the following error: The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should i do? Is there a better solution?

I try to count unread messages for a user. On my model i have a property, LastMessageDate that contains the date on the last created message in the group chat. I have also a Members propeerty (list) that contains the members in the group chat. Each member has a UserId and LastReadDate property. The LastReadDate is updated when the user write a new message in the group chat or when the user loads messages from the group chat.

Now i want to count the number of chats where a specific user has unreaded messages (The messages is stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But i recive error: The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should i do? Is there a better solution?

I try to count unread messages for a user. On my model i have a property, LastMessageDate that contains the date on the last created message in the group chat. I have also a Members propeerty (list) that contains the members in the group chat. Each member has a UserId and LastReadDate property. The LastReadDate is updated when the user write a new message in the group chat or when the user loads messages from the group chat.

Now i want to count the number of chats where a specific user has unreaded messages (The messages is stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But i receive the following error:

The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should i do? Is there a better solution?

Source Link
mrcode
  • 567
  • 2
  • 6
  • 19

Count unread messages in MongoDb

I try to count unread messages for a user. On my model i have a property, LastMessageDate that contains the date on the last created message in the group chat. I have also a Members propeerty (list) that contains the members in the group chat. Each member has a UserId and LastReadDate property. The LastReadDate is updated when the user write a new message in the group chat or when the user loads messages from the group chat.

Now i want to count the number of chats where a specific user has unreaded messages (The messages is stored in another collection). I try this:

var db = GetGroupCollection();

var filter = Builders<ChatGroup>.Filter.Where(p => p.Members.Any(m => m.UserId == userId && m.LastReadDate < p.LastMessageDate));
return await db.CountDocumentsAsync(filter);

But i recive error: The LINQ expression: {document}{Members}.Where((({document}{UserId} == 730ddbc7-5d03-4060-b9ef-2913d0b1d7db) AndAlso ({document}{LastReadDate} < {document}{LastMessageDate}))) has the member "p" which can not be used to build a correct MongoDB query.

What should i do? Is there a better solution?