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")
}
_idof the document. The answer makes that point more clear now with a slight edit.