Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Mongo query to find sub document with condition

Tags:

mongodb

I have a document in a collection like this, I need to find the record with form_Id:1 and Function_Id:2, how to write the mongo query.

"Form_Id" : 1,
"Function" : [{
  "Function_Id" : 1,
  "Role" : [{
      "Role_Id" : 1,
      "UserId" : ["Admin", "001"]
    }]
}, {
  "Function_Id" : 2,
  "Role" : [{
      "Role_Id" : 2,
      "UserId" : ["Admin", "005"]
    }]
}]
like image 778
siva Avatar asked Feb 26 '13 04:02

siva


People also ask

What are subdocuments in MongoDB?

In Mongoose, subdocuments are documents that are nested in other documents. You can spot a subdocument when a schema is nested in another schema. Note: MongoDB calls subdocuments embedded documents. In practice, you don’t have to create a separate childSchema like the example above.

What happens if we don’t use the find method in MongoDB?

If we don’t use these two parameters then the find method will return all the documents available within the MongoDB collection. Query – This is an optional parameter which defines the selection criteria.

How to fetch a specific document from the MongoDB collection?

In order to fetch a specific document from the MongoDB collection, we can use a query parameter in the find method. This parameter will help us to fetch only that record which meets the criteria. In the following example, we are going to fetch the record by staff ID, where the staff ID is equal three and it will return us only that document.

What is compound query in MongoDB?

Refer to the Query and Projection Operators document for the complete list of MongoDB query operators. A compound query can specify conditions for more than one field in the collection's documents.


2 Answers

You can use dot notation and the $ positional projection operator to do this:

db.test.find({Form_Id: 1, 'Function.Function_Id': 2}, {_id: 0, 'Function.$': 1})

returns:

{"Function": [{"Function_Id": 2, "Role": [{"Role_Id": 2, "UserId": ["Admin", "005"]}]}]}
like image 192
JohnnyHK Avatar answered Oct 23 '22 04:10

JohnnyHK


Since your function key is an array, in order to use the $match operator, first you have to use the $unwind operator. http://docs.mongodb.org/manual/reference/aggregation/unwind/ And then you use $match operator to find the documents that you want http://docs.mongodb.org/manual/reference/aggregation/match/

So your query should look like this

    db.collection.aggregate([{$unwind:"$Function"},{$match:{"Form_id":1,"Function.Function_id":2}}])

By default mongo will display the _id of the document. So if you do not want to display the _id, after matching the relevant ones, you could use the $project operator http://docs.mongodb.org/manual/reference/aggregation/project/

     db.collection.aggregate([{$unwind:"$Function"},{$match:{"Form_id":1,"Function.Function_id":2}},{$project:{"_id":0,"Form_id":1,"Function":1}}])

If you don't want the form_id to be displayed, simply don't specify the form_id in the project part of the query. By default mongo will only display the keys whose value is 1. If the key is not mentioned it will not display it.

    db.collection.aggregate([{$unwind:"$Function"},{$match:{"Form_id":1,"Function.Function_id":2}},{$project:{"_id":0,"Function":1}}])
like image 32
ann Avatar answered Oct 23 '22 04:10

ann