1

I have chats's collection with participants array

    [{
    "_id": ObjectId("5d12b2a10507cfe0bad6d93c"),
    "participants": [{
            "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"),
            "firstname": "John",
            "lastname": "Anderson",
            "icon": "/assets/images/avatars/small/2.jpg"
        },
        {
            "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"),
            "firstname": "John",
            "lastname": "Anderson",
            "icon": "/assets/images/avatars/small/2.jpg"
        }
    ]
}, {
    "_id": ObjectId("5d1124a50507cfe0baba7909"),
    "participants": [{
            "_id": ObjectId("5ce4af580507cfe0ba1c6f5b"),
            "firstname": "John",
            "lastname": "Anderson",
            "icon": "/assets/images/avatars/small/2.jpg"
        },
        {
            "_id": ObjectId("5ce54cb80507cfe0ba25d74b"),
            "firstname": "Milosh",
            "lastname": "Jersi",
            "icon": "/assets/images/avatars/small/3.jpg"
        }
    ]
}]

I fetch it by req.db.collection('chats').findOne({'participants._id': {$all: [req.userID, new mongo.ObjectID(req.params.to)]}}); where userID is also ObjectID and equals.

Usually it have different participants, but our user can also send messages to itself, is allowed option in many social networks. So in this situation, our user "John Anderson" sent message to himself and we inserted chat document for it.

And now i have problem, how to get document with equal array values

{'participants._id': { '$all': [ 5ce4af580507cfe0ba1c6f5b, 5ce4af580507cfe0ba1c6f5b] }}
// return every chat contains our id in atleast one field, but we need both to be equal
// same for $in

{'participants._id': { '$eq': [ 5ce4af580507cfe0ba1c6f5b, 5ce4af580507cfe0ba1c6f5b] }}
// return nothing

what else can I do ?

2
  • Can you share you collection with chat data. Commented Jun 26, 2019 at 4:17
  • I updated first code block in post, is chat collection`s data, just document with array of users in chat Commented Jun 26, 2019 at 17:00

1 Answer 1

2

you can achieve this with the aggregation framework, using a $group stage. First, group by chat._id and use $addToSet to keep only unique users in a new array, ant then add a filter to keep only the documents with one participant:

db.collection.aggregate([
  {
    "$unwind": "$participants"
  },
  {
    "$group": {
      "_id": "$_id",
      "participants": {
        "$addToSet": "$participants._id"
      }
    }
  },
  {
    "$match": {
      "participants": {
        "$size": 1
      }
    }
  }
])

result:

[
  {
    "_id": ObjectId("5d12b2a10507cfe0bad6d93c"),
    "participants": [
      ObjectId("5ce4af580507cfe0ba1c6f5b")
    ]
  }
]

you can try it online: mongoplayground.net/p/plB-gsNIxRd

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think its a bit cumbersome for so simple task but probably mongo not have other ways( But it also find all chats with one participant, how can i filter it by certain participants[0]._id?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.