why
why

Reputation: 195

Mongodb array structure

Theres something here I can't quite figure out.

When I attempt to query an object with several fields I yield no results. The object structure looks like this:

{ 
    "_id" : ObjectId("4d8b55f017a7303b0b000000"), 
    "title" : "Apollo", 
    "body" : "A spaceflight mission to the moon", 
    "tags" : [ [ "moon", "space", "nasa", "mission" ] ] 
}

This is my query:

db.test.find({ tags: { $all: ['moon', 'mission'] } })   

However I do get result by creating a new object with a single field:

{ 
    "_id" : ObjectId("4d8b9e5935037b3c8228709c"), 
    "tags" : [ "apple", "banana", "pear" ] 
}

... with the same query as the one above.

['tags'] isn't nested inside any other array, so why does it not return my search queries? Please enlighten me.

Sincerely, Why

Upvotes: 1

Views: 1858

Answers (2)

jhavrda
jhavrda

Reputation: 395

db.test.find({ tags: { $all: [ ['moon', 'mission'] ] } })  

Upvotes: 0

user2665694
user2665694

Reputation:

Why are you using a nested array

"tags" : [ [ "moon", "space", "nasa", "mission" ] ]

here?

This does not make any sense.

Upvotes: 3

Related Questions