0

I have a document in my collection.

"_id" : ObjectId("5755b94abcfc6666d6f5fe56"),
"NAME" : John Smith,
"PHONE" : "800-555-1000",
"DOC" : {
    " TYPE" : "M",
    " DATE" : "01/28/2016",
    " AMOUNT" : 281593
},
"ADDRESS" : 123 Sesame Street

I am trying to find this record by passing in the keys and values:

db.acris.find("DOC" : {" TYPE" : "MTGE"," DATE" : "01/28/2003"," AMOUNT" : 281593})

However, I am getting the following error:

SyntaxError: Unexpected token :

How can I fix this query so that the code will work?

0

2 Answers 2

1

The syntax error is fixed as KRONWALLED answer shows. The query I think you want is however is:

db.acris.find({"DOC.TYPE" : "M", "DOC.DATE" : "01/28/2003"," DOC.AMOUNT" : 281593})

Notice the DOT notation for matching against subfields.

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

Comments

1

You're missing {} around the whole find like

db.collection.find({ "DOC" : ... })

Update: Just to be complete. You can also search for the whole element. You don't necessarily need dot notation.

> db.test.insertOne({"NAME" : "John Smith",
... "PHONE" : "800-555-1000",
... "DOC" : {
...     " TYPE" : "M",
...     " DATE" : "01/28/2016",
...     " AMOUNT" : 281593
... },
... "ADDRESS" : "123 Sesame Street"})

> db.test.find({"DOC" : { " TYPE" : "M", " DATE" : "01/28/2016", " AMOUNT" : 281593 }})
{ "_id" : ObjectId("5759a933cdcc5a8d09a1aeb9"), "NAME" : "John Smith", "PHONE" : "800-555-1000", "DOC" : { " TYPE" : "M", " DATE" : "01/28/2016", " AMOUNT" : 281593 }, "ADDRESS" : "123 Sesame Street" }

Comments

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.