0
"dammfield" : [
        {
            "_id" : ObjectId("60705d602ba9490026482370"),
            "f1" : ISODate("2021-04-09T10:57:52.152-03:00"),
            "f2" : ISODate("2021-04-09T10:57:52.152-03:00"),
            "f3" : ISODate("2022-04-09T10:57:52.152-03:00"),
            "f4" : ObjectId("5efa2c4977d67e002763c5ee"),
            "f5" : true,
            "f6" : "VALID",
            "f7" : {
                "_id" : ObjectId("5ef39d0c80478000278879e5"),
                "ff1" : "123123123",
                "ff2" : "9812378921738127839"
            }
        }
    ],

I would like to know how can i filter elements inside an array that have another element.

i'm trying everything:

db.mycollection.find({ dammfield: { $elemMatch: { "f7.ff1": "123123123" }}})


db.mycollection.find({
        dammfield: {
            $exists: true,
            $ne: []
        }
    }, {
        dammfield: 1
    })
    .sort({
        _id: -1
    })
    .limit(1000)
    

How can I filter all documents that have dammfield.f7.ff1 = something?

1

You can filter the array using the $elemMatch projection operator (this is different from the $elemMatch query operator).

db.collection.find( { }, { dammfield: { $elemMatch: { "f7.ff1": "123123123" } } } )

You can also use the aggregate operator $filter with the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.