How to get a specific embedded document inside a M

2019-01-07 08:43发布

问题:

This question already has an answer here:

  • Retrieve only the queried element in an object array in MongoDB collection 11 answers

I have a collection Notebook which has embedded array document called Notes. The sample

document looks like as shown below.

{
"_id" : ObjectId("4f7ee46e08403d063ab0b4f9"),
"name" : "MongoDB",
"notes" : [
            {
              "title" : "Hello MongoDB",
              "content" : "Hello MongoDB"
            },
            {
              "title" : "ReplicaSet MongoDB",
              "content" : "ReplicaSet MongoDB"
            }
         ]
}

I want to find out only note which has title "Hello MongoDB". I am not getting what should

be the query. Can anyone help me.

回答1:

Outdated answer: See the other answers.


I don't believe what you are asking is possible, at least without some map-reduce maybe?

See here: Filtering embedded documents in MongoDB

That answer suggests you change your schema, to better suit how you'd like to work with the data.

You can use a either "dot notation" or $elemMatch to get back the correct, document that has the matching "note title" ...

> db.collection.find({ "notes.title" : "Hello MongoDB"}, { "notes.title" : 1"});

or ...

> db.collection.find({ "notes" : { "$elemMatch" : { "title" : "Hello MongoDB"} }});

But you will get back the whole array, not just the array element that caused the match.

Also, something to think about ... with your current setup it woud be hard to do any operations on the items in the array.

If you don't change your schema (as the answer linked to suggests) ... I would consider adding "ids" to each element in the array so you can do things like delete it easily if needed.



回答2:

You can do this with mongo version higher 2.2

the query like this:

db.coll.find({ 'notes.title': 'Hello MongoDB' }, {'notes.$': 1});

you can try with $elemMatch like Justin Jenkins



回答3:

You can do this in MongoDb version 3.2+ with aggregation.

Query:

db.Notebook.aggregate(
    {
        $project: {
            "notes": {
                $filter: {
                    input: "$notes",
                    as: "note",
                    cond: { 
                        $eq: [ "$$note.title", "Hello MongoDB" ]
                    }
                }
            }
        }
    }
)

Result:

{ 
    "_id" : ObjectId("4f7ee46e08403d063ab0b4f9"), 
    "notes" : [ 
        { 
            "title" : "Hello MongoDB", 
            "content" : "Hello MongoDB" 
        } 
    ] 
}

$$ used here to access the variable. I used here to access the newly created note variable inside the $filter.

You can find additional details in the official documentation about $filter, $eq and $$.

$filter: Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.

$eq: Compares two values and returns true/false when the values are equivalent or not (...).

$$: Variables can hold any BSON type data. To access the value of the variable, use a string with the variable name prefixed with double dollar signs ($$).


Note:

Justin Jenkin's answer is outdated and kop's answer here doesn't return multiple documents from the collection. With this aggregation query, you can return multiple documents if needed.

I needed this and wanted to post to help someone.



回答4:

You can use $ or $elemMatch. The $ operator and the $elemMatch operator project a subset of elements from an array based on a condition.

The $elemMatch projection operator takes an explicit condition argument. This allows you to project based on a condition not in the query.

db.collection.find(
    {
        // <expression>
    },
    {
        notes: {
            $elemMatch: {
                title: 'Hello MongoDB'
            }
        },
        name: 1
    }
)

The $ operator projects the array elements based on some condition from the query statement.

db.collection.find(
    {
        'notes.title': 'Hello MongoDB'
    },
    {
        'notes.title.$': 1,
         name: 1
    }
)


回答5:

You can perform the query like this:

db.coll.find({ 'notes.title': 'Hello MongoDB' });

You can also refer to the docs for more details.



标签: mongodb