2

I have a collection as follows:

{
    "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"),
    "date" : "20180126",
    "oid" : "000",
    "aid" : "0000000000",
    "currentavalible" : "7",
    "deletedbyuser" : "0",
    "reply" : [
        {
            "oidaid" : "000000",
            "nickname" : "****",
            "date" : "2018-01-26 07:32:45",
            "recomm" : "13",
            "unrecomm" : "0",
            "contents" : "aaaaaaaaaaaa"
        }, 
        {
            "oidaid" : "000088",
            "nickname" : "para****",
            "date" : "2018-01-26 08:29:54",
            "recomm" : "12",
            "unrecomm" : "0",
            "contents" : "bbbbbb"
        }
    ]
}

To find in reply that "recomm" is "13" i tried:

db.comments.find( { 'reply.recomm' : '13' })
db.comments.find( { 'reply' :{ 'recomm' : '13'}  })
db.comments.find( {'_id' :  ObjectId("5a6d9a1ee183f06dfd91dbf0"), 'reply.recomm' : '12' })
db.comments.find( {'_id' :  ObjectId("5a6d9a1ee183f06dfd91dbf0"), 'reply' : { 'recomm' : '12'} })

And all fetched me nothing.

What seems to be the error in my syntax?

2
  • db.collection.find() , A cursor to the documents that match the query criteria. When the find() method “returns documents,” the method is actually returning a cursor to the documents. – Md Haidar Ali Khan Jan 28 '18 at 11:27
  • Use the $match(aggregation) pipeline . – Md Haidar Ali Khan Jan 28 '18 at 11:30
1
db.comments.find( { 'reply.recomm' : '13' })
db.comments.find( {'_id' :  ObjectId("5a6d9a1ee183f06dfd91dbf0"), 'reply.recomm' : '12' })

Both above query is looks fine and will be fetch the documents of records inside the comments collections.

I am sure that you are doing mistake something like not in the tree of particular database where your comments collection reside.

Let's from beginning see here,

First i have create the StackExchange database.

>Use StackExchange

Then i have created the comments collection inside the StackExchange database.

> use StackExchange
switched to db StackExchange
>

> db.createCollection("comments")
{ "ok" : 1 }

And inside of comments collections i have insert the documents, which you have provided in souce code.

> db.comments.insert({
...     "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"),
...     "date" : "20180126",
...     "oid" : "000",
...     "aid" : "0000000000",
...     "currentavalible" : "7",
...     "deletedbyuser" : "0",
...     "reply" : [
...         {
...             "oidaid" : "000000",
...             "nickname" : "****",
...             "date" : "2018-01-26 07:32:45",
...             "recomm" : "13",
...             "unrecomm" : "0",
...             "contents" : "aaaaaaaaaaaa"
...         },
...         {
...             "oidaid" : "000088",
...             "nickname" : "para****",
...             "date" : "2018-01-26 08:29:54",
...             "recomm" : "12",
...             "unrecomm" : "0",
...             "contents" : "bbbbbb"
...         }
...     ]
... })
WriteResult({ "nInserted" : 1 })

Now i want to fetch comments documents records from the comments collection, which is in StackExchange database. First check through mongo shell, what is your current database like that

> db
StackExchange
>

Here it mongo shell the current database is StackExchange.

Note:- I have create comments collection in StackExchange database. But for your confirmation , you must always checks before queering the query in mongo shell like

show collections comments

Then try to find the documents inside the collection through the db.collection.find().

Note:- Here collection is the collection name. In your case it's comments.

From the first query db.comments.find( { 'reply.recomm' : '13' })

> db.comments.find( { 'reply.recomm' : '13' }).pretty()
{
        "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"),
        "date" : "20180126",
        "oid" : "000",
        "aid" : "0000000000",
        "currentavalible" : "7",
        "deletedbyuser" : "0",
        "reply" : [
                {
                        "oidaid" : "000000",
                        "nickname" : "****",
                        "date" : "2018-01-26 07:32:45",
                        "recomm" : "13",
                        "unrecomm" : "0",
                        "contents" : "aaaaaaaaaaaa"
                },
                {
                        "oidaid" : "000088",
                        "nickname" : "para****",
                        "date" : "2018-01-26 08:29:54",
                        "recomm" : "12",
                        "unrecomm" : "0",
                        "contents" : "bbbbbb"
                }
        ]
}

and from the query db.comments.find( {'_id' : ObjectId("5a6d9a1ee183f06dfd91dbf0"), 'reply.recomm' : '12' })

> db.comments.find( {'_id' :  ObjectId("5a6d9a1ee183f06dfd91dbf0"), 'reply.recomm' : '12' }).pretty()
{
        "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"),
        "date" : "20180126",
        "oid" : "000",
        "aid" : "0000000000",
        "currentavalible" : "7",
        "deletedbyuser" : "0",
        "reply" : [
                {
                        "oidaid" : "000000",
                        "nickname" : "****",
                        "date" : "2018-01-26 07:32:45",
                        "recomm" : "13",
                        "unrecomm" : "0",
                        "contents" : "aaaaaaaaaaaa"
                },
                {
                        "oidaid" : "000088",
                        "nickname" : "para****",
                        "date" : "2018-01-26 08:29:54",
                        "recomm" : "12",
                        "unrecomm" : "0",
                        "contents" : "bbbbbb"
                }
        ]
}
>

But from the mention below both query you shall not fetch the documents records from the comments collection due to their improper fetching of Array records.

db.comments.find( { 'reply' :{ 'recomm' : '13'}  })

db.comments.find( {'_id' :  ObjectId("5a6d9a1ee183f06dfd91dbf0"), 'reply' : { 'recomm' : '12'} })

As per MongoDB 3.6 Documented Here . If you want to fetch the array documents then you can use $ (projection), Or you can use Aggregation Pipeline Stages.

As you said only you want to find the records in reply that "recomm" is 13. so, you have to use $elemMatch (projection)operator and your query will be as mention below

> db.comments.find({"_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0")},{reply: {$elemMatch: {recomm: "13"}}}).pretty()
{
        "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"),
        "reply" : [
                {
                        "oidaid" : "000000",
                        "nickname" : "****",
                        "date" : "2018-01-26 07:32:45",
                        "recomm" : "13",
                        "unrecomm" : "0",
                        "contents" : "aaaaaaaaaaaa"
                }
        ]
}
>

After the OP requirement to edit the code and execute the query to find out OP desire results.

Here i want to insert the second documents which having different id field records in comments documents.

> db.comments.insert({
... ...     "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf1"),
... ...     "date" : "20180126",
... ...     "oid" : "000",
... ...     "aid" : "0000000000",
... ...     "currentavalible" : "7",
... ...     "deletedbyuser" : "0",
... ...     "reply" : [
... ...         {
... ...             "oidaid" : "000000",
... ...             "nickname" : "****",
... ...             "date" : "2018-01-26 07:32:45",
... ...             "recomm" : "13",
... ...             "unrecomm" : "0",
... ...             "contents" : "aaaaaaaaaaaa"
... ...         },
... ...         {
... ...             "oidaid" : "000088",
... ...             "nickname" : "para****",
... ...             "date" : "2018-01-26 08:29:54",
... ...             "recomm" : "12",
... ...             "unrecomm" : "0",
... ...             "contents" : "bbbbbb"
... ...         }
... ...     ]
... ... })
WriteResult({ "nInserted" : 1 })

Now i only find out the comments collection documents , which having the "recomm":"13" records.

> db.comments.find({},{reply: {$elemMatch: {recomm: "13"}}}).pretty()
{
        "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"),
        "reply" : [
                {
                        "oidaid" : "000000",
                        "nickname" : "****",
                        "date" : "2018-01-26 07:32:45",
                        "recomm" : "13",
                        "unrecomm" : "0",
                        "contents" : "aaaaaaaaaaaa"
                }
        ]
}
{
        "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf1"),
        "reply" : [
                {
                        "oidaid" : "000000",
                        "nickname" : "****",
                        "date" : "2018-01-26 07:32:45",
                        "recomm" : "13",
                        "unrecomm" : "0",
                        "contents" : "aaaaaaaaaaaa"
                }
        ]
}
7
  • Thanks, I perfectly understood! Can you answer me one last question? What should I do to not only search in "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0") this object but all objects in db.comments? – Arthur Park Jan 28 '18 at 12:40
  • @Arthur Park, If you want to use 'id' field and you want to search all documents in your 'comments' collection then simple query like "db.comments.find().pretty()". It will search all documents in your comments collection. – Md Haidar Ali Khan Jan 28 '18 at 12:44
  • Sorry for the basic question, but I meant if i am to search with db.comments.find({"_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0")},{reply: {$elemMatch: {recomm: "13"}}}), It will search from object 5a6d9a1ee183f06dfd91dbf0 only. What should I do to search recomm: "13" to all object in collection? I am literally digging into the documentation but I can't understand it :( – Arthur Park Jan 28 '18 at 12:48
  • @ArthurPark, Then simple execute the query like db.comments.find({},{reply: {$elemMatch: {recomm: "13"}}}).pretty() – Md Haidar Ali Khan Jan 28 '18 at 12:53
  • Thanks so much I was digging out literaly everywhere to find answers. – Arthur Park Jan 28 '18 at 12:57
0

Cannot find error.. I inserted that same document to test.comments and firing your query gave right answer.

db.comments.find({'reply.recomm':'13'})
{ "_id" : ObjectId("5a6d9a1ee183f06dfd91dbf0"), "date" : "20180126", "oid" : "000", "aid" : "0000000000", "currentavalible" : "7", "deletedbyuser" : "0", "reply" : [ { "oidaid" : "000000", "nickname" : "****", "date" : "2018-01-26 07:32:45", "recomm" : "13", "unrecomm" : "0", "contents" : "aaaaaaaaaaaa" }, { "oidaid" : "000088", "nickname" : "para****", "date" : "2018-01-26 08:29:54", "recomm" : "12", "unrecomm" : "0", "contents" : "bbbbbb" } ] }

Are you sure that you were in the right database?

So db command gives right DB name. Or db.comments.find() gives you all documents from your comments collection.

2
  • I did the queries to get ` "recomm" : "13"` . but it gave all 12 and 13 – Arthur Park Jan 28 '18 at 12:07
  • @Arthur Park, I have replied for that, if you want to fetch only the records of " recomm": "13" the use the "$elemMatch (projection)" operator. Which only fetch the records based on their condition. – Md Haidar Ali Khan Jan 28 '18 at 12:40

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.