0

I just wonder how I can get an object within an array of objects. I just need that object.

This is the collection:

{
    "net" : "192.168.1.1/24",
    "_id" : ObjectId("531d1c2d857831021c48e3af"),
    "ips" : [ 
        {
            "ips" : "192.168.1.1",
            "_id" : ObjectId("531d1c2d857831021c48e3b3")
        }, 
        {
            "ips" : "192.168.1.33",
            "_id" : ObjectId("531d1c2d857831021c48e3b2")
        }, 
        {
            "ips" : "192.168.1.38",
            "_id" : ObjectId("531d1c2d857831021c48e3b1")
        }, 
        {
            "ips" : "192.168.1.106",
            "_id" : ObjectId("531d1c2d857831021c48e3b0")
        }
    ],
    "__v" : 0
}

I need the object with "531d1c2d857831021c48e3b0" ID, what is the sentence to get only that object? I tried with db.nets.find({ "ips._id": ObjectId("531d1c2d857831021c48e3b3") } ) but I get the whole collection.

Thank you very much.

Edit. With the code I receive this:

db.nets.find(
      { 
     "_id": ObjectId("531d1c2d857831021c48e3af"),
     "ips._id": ObjectId("531d1c2d857831021c48e3b3") 
 },
 { "ips.$" : 1 }
 )

{
    "_id" : ObjectId("531d1c2d857831021c48e3af"),
    "ips" : [ 
        {}, 
        {}, 
        {}, 
        {}
    ]
}

but what I want to receive is this:

{
            "ips" : "192.168.1.33",
            "_id" : ObjectId("531d1c2d857831021c48e3b2")
        }
3
  • Your added response is not possible in current versions. What version of MongoDB are you running? Or did you actually do a find or findOne without specifying the primary _id of the document. The answer makes that point more clear now with a slight edit. Commented Mar 13, 2014 at 13:31
  • I'm using MongoDB version: 2.0.6. I checked it in console writing "mongo". Commented Mar 13, 2014 at 14:21
  • yeaaaaah!!!!!! It was the version!!! Now I have 2.4.9 version and it works perfectly. Thank you very munch!! Commented Mar 13, 2014 at 14:34

1 Answer 1

1

Use projection. The positional $ operator allows you to just select the matching index of the array.

 db.nets.find(
     { 
         "_id": ObjectId("531d1c2d857831021c48e3af"),
         "ips._id": ObjectId("531d1c2d857831021c48e3b3") 
     },
     { "ips.$" : 1 }
 )

Which gives this response:

{
    "_id" : ObjectId("531d1c2d857831021c48e3af"),
    "ips" : [
            {
                    "ips" : "192.168.1.1",
                    "_id" : ObjectId("531d1c2d857831021c48e3b3")
            }
    ]
}

It's not the "whole collection", it's the "whole document", in which unless you filter the fields with a "projection" you will get by default.

Sign up to request clarification or add additional context in comments.

4 Comments

@user2976034 I think you just tried to post a multi-line response in a comment. It didn't work. But of course I do test things so, yes this will only return one array element. Which is what you are asking for are you not?
Hello Neil. Sorry, I sent the last comment without writing completely. I wanted to write: Thank you for your explanation about $ operator, I didn't know it (I'm a complete newbie). With the code you wrote, I don't receive what I want. I think I don't explain correctly. Check the first post to see what I receive and the object that I want to receive if it's possible. Thank you very much for your time.
@user2976034 as I explained. I think your result is because you did not filter the document that actually has this array member in it. You need to do that.
Hi again Neil, I did a screenshot because it gives to me other result than you! oi59.tinypic.com/34dhfyt.jpg

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.