Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7702719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:17:27+00:00 2026-05-31T23:17:27+00:00

I have a collection for conversations: {_id: …, from: userA, to: userB, message: Hello!,

  • 0

I have a collection for conversations:

{_id: ..., from: userA, to: userB, message: "Hello!", datetime: ...}

I want to show a preview of user’s conversations – last message from each conversation between current user and any other users. So when user clicks on some “last message” he goes to next page with all messages between him and that user.

How do I do that (get 1 last message from each conversation) without Map/Reduce?

1) use “distinct” command? (how?)

2) set “last” flag for last message? I think it’s not very safe…

3) ..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:17:29+00:00Added an answer on May 31, 2026 at 11:17 pm

    I was writing up a complicated answer to this question using cursors and a lot of advanced query features and stuff… it was painful and confusing. Then I realized, this is painful because it’s not how mongodb expects you to do things really.

    What I think you should do is just denormalize the data and solve this problem in one shot easily. Here’s how:

    • Put a hash/object field on your User called most_recent_conversations
    • When you make a new conversation with another user, update it so that it looks like this:

      previewUser.most_recent_conversations[userConversedWith._id] = newestConversation._id
      
    • Every time you make a new conversation, simply smash the value for the users involved in their hashes with the newer conversation id. Now we have a { uid: conversationId, ... } structure which basically is the preview data we need.

    • Now you can look up the most recent conversation (or N conversations if you make each value of the hash an array!) simply:

      var previewUser = db.users.findOne({ _id: someId });
      var recentIds = [];
      for( uid in previewUser.most_recent_conversations ) {
        recentIds.push( previewUser.most_recent_conversations[uid] );
      }
      var recentConversations = db.conversations.find({
        _id: { $in: recentIds }
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Collection List<Car> . How to compare each item from this collection with
I have a collection of classes that inherit from an abstract class I created.
I have a collection of classes that I want to serialize out to an
I have a collection of records in my database that I want to print
I have collection of documents Message in RavenDB. Definition: class Message { string Content;
Hello guys i have collection : public class ActionData { private int iD; public
i have collection of classes in one form and i want to use the
I have a collection IQueryable<Car> and I want to pass it into a generic
I'm planning to have collection of items stored in a TCollection. Each item will
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.