I was wondering if this is possible, or do I need to use the aggregation pipeline instead?
I have read posts such as this, and intuitively feel like it's possible.
Example of docs:
{
"_id": ObjectID("5143ddf3bcf1bfab37d9c6f"),
"permalink": "btxcacmbqkxbpgtpeero",
"author": "machine",
"title": "Declaration of Independence",
"tags": [
"study",
"law"
],
"comments": [
{
"body": "comment 1",
"email": "email_1@test.com",
"author": "machine_1"
},
{
"body": "comment 2",
"email": "email_2@test.com",
"author": "machine_2"
},
{
"body": "comment 3",
"email": "email_3@test.com",
"author": "machine_3"
},
]
"date": ISODate("2013-03-16T02:50:27.878Z")
}
I am trying to access a particular comment in "comments" by it's index position using dot notation in the projection field, with the following:
db.collection.find({permalink: "btxcacmbqkxbpgtpeero"}, {'comments.0.1.': 1})
Where comments.0
is the first item in the field: the array, and .1
is the second comment in the array.
The result I am getting:
{ "_id" : ObjectID("5143ddf3bcf1bfab37d9c6f"), "comments" : [ { }, { }, { } ] }
If I take away the .1
, leaving just comments.0
, I get the same result:
{ "_id" : ObjectID("5143ddf3bcf1bfab37d9c6f"), "comments" : [ { }, { }, { } ] }
If I take away the .0
, leaving just comments
, I get the comments still inside their array:
[
{
"body": "comment 1",
"email": "email_1@test.com",
"author": "machine_1"
},
{
"body": "comment 2",
"email": "email_2@test.com",
"author": "machine_2"
},
{
"body": "comment 3",
"email": "email_3@test.com",
"author": "machine_3"
}
]
Can this be done? If so, how?