Search code examples
arraysmongodbtypescriptsubdocument

Query nested arrays of subdocuments


EDIT: The query should not user the '&" characters. Those or for updates.

ANSWER: Courtesy of Gibbs

correct: 
const result : any = await mongoose.model('Events').update(
    {
        _id: eventId,
        "groups._id": groupId,
        "groups.users._id": userId
    },

incorrect:
const result: any = await mongoose.model('Events').findOne(
    {
        _id: eventId,
        "groups._id": "5f270416e7964b20d6f8953e",
        "groups.$.users": { _id: '5f270877b4942d2528885dbd' }
    })
    // OR:
    {
        _id: eventId,
        "groups._id": "5f270416e7964b20d6f8953e",
        "groups.$.users._id" : '5f270877b4942d2528885dbd',  
    })

With only the first two predicates,(eventId and groups [$_id]) it returns the event. But the third breaks.

Does anybody know why this returns null (object ids are copied directly from the db) Thanks!

{
    "_id": ObjectId("5f270416e7964b20d6f8953d"),
    "lastRounds": [],
    "name": "EpicFest",
    "start": 1596392488896.0,
    "end": 1596392500896.0,
    "groups": [
      {
        "_id": ObjectId("5f270416e7964b20d6f8953e"),
        "name": "Vossius",
        "users": [
          {
            "created": 1596393590778.0,
            "sessionId": null,
            "feedBack": {
              "messagesSent": 0,
              "messagesSentLiked": 0,
              "messagesReceived": 0,
              "messagesReceivedLiked": 0,
              "userFeedbackReceived": [],
              "chatFeedbackReceived": [],
              "_id": ObjectId("5f270877b4942d2528885dbe")
            },
            "_id": ObjectId("5f270877b4942d2528885dbd"),
            "image": "someAvatr",
            "name": "hans",
            "groupId": "5f270416e7964b20d6f8953e",
            "results": []
          },
          
        ]
      },
      {
        "_id": ObjectId("5f270416e7964b20d6f8953f"),
        "users": [],
        "name": "Ignatius"
      }
    ],
    "results": [],
    "theses": [],
    "rounds": 10,
    "schedule": [],
    "__v": 4
}

Solution

  • Mongo Play

    db.collection.find({
      _id: ObjectId("5f270416e7964b20d6f8953d"),
      "groups._id": ObjectId("5f270416e7964b20d6f8953e"),
      "groups.users._id": ObjectId("5f270877b4942d2528885dbd")
    })
    

    You tried to use String "5f270416e7964b20d6f8953e". It should be ObjectId

    You can use . notation to access a nested element or $elemMatch