0

I've this object on reference collection:

> db.reference.find().pretty();
{
    "_id" : "Ref1",
    "metadata" : {
        "values" : [
            {
                "hola" : "adeu"
            },
            {
                "departament" : [
                    {
                        "ambit" : "just",
                        "name" : "ts"
                    }
                ]
            }
        ]
    },
    "timestamp" : ISODate("2018-09-22T08:37:08.989Z"),
    "_class" : "com.Reference"
}

I need to get document where metadata.values has an object {hola: "adeu"}.

I've tried without any luck:

> db.reference.find({metadata: {values: {hola: "adeu"}}});
> db.reference.find({metadata: {values: [{hola: "adeu"}]}});

Any ideas?

3
  • Try with db.reference.find({metadata: {values: { $elemMatch: {hola: "adeu"}}}}); Commented Sep 22, 2018 at 10:04
  • It doesn't work. Commented Sep 22, 2018 at 10:12
  • You can try : db.getCollection('reference').find({"metadata.values.hola" : "adeu"});, Also This lik help you with aggregate : stackoverflow.com/questions/52404430/… Commented Sep 22, 2018 at 12:44

1 Answer 1

1

You can do something like this:

db.getCollection('reference').find({"metadata.values" : { "$in" : [{"hola" : "adeu"}] }});

You can also do an elemMatch like this, when you want it for a single object:

db.getCollection('reference').find({"metadata.values" : { "$elemMatch" : {"hola" : "adeu"} }})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.