0

I have a collection with documents are like this:

{
    "date" : 20200817,
    "items" : [ 
        {
            "name" : "item1", "values" : ["val1", "val2", "val3"], "values2" : ["val21", "val22", "val23"]
        }, 
        {
            "name" : "item2", "values" : ["val1", "val4"]
        }, 
        {
            "name" : "item3", "values" : ["val1", "val3"], "values2" : ["val31", "val33"]
        }
    ]
}

I want to get values and values2 from items that name is item3 like this

{"values" : ["val1", "val3"], "values2" : ["val31", "val33"]}

I have this query :

db.test.find(
   {'items.name': 'item3'}, {'items.$.values': 1, 'items.$.values2': 1})

but i get this error

Cannot specify more than one positional proj. per query.

Where is my query wrong?

What can I do?

Thanks

1
  • you can't with find(), but you can do this way, otherwise you need to use aggregate() Commented Sep 3, 2020 at 13:08

1 Answer 1

1
//data set queried here for clarity
> db.items5.find().pretty();
{
        "_id" : ObjectId("5f5109ef3372ab5da3a605ba"),
        "date" : 20200817,
        "items" : [
                {
                        "name" : "item1",
                        "values" : [
                                "val1",
                                "val2",
                                "val3"
                        ],
                        "values2" : [
                                "val21",
                                "val22",
                                "val23"
                        ]
                },
                {
                        "name" : "item2",
                        "values" : [
                                "val1",
                                "val4"
                        ]
                },
                {
                        "name" : "item3",
                        "values" : [
                                "val1",
                                "val3"
                        ],
                        "values2" : [
                                "val31",
                                "val33"
                        ]
                }
        ]
}
//actual MongoDB query and query output using aggregate
> db.items5.aggregate([
... {$unwind:"$items"},
... {$match:{"items.name":"item3"}},
... {$project:{"items.values":1,
...            "items.values2":2}}
... ]).pretty();
{
        "_id" : ObjectId("5f5109ef3372ab5da3a605ba"),
        "items" : {
                "values" : [
                        "val1",
                        "val3"
                ],
                "values2" : [
                        "val31",
                        "val33"
                ]
        }
}
>
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.