1

My collection has the following structure:

- file_path (string)
- file_contents(object)
    - a[] (array)
    - b[] (array)

An example document could be like:

  • file_path : "/username/innerdirectory/file.txt"
  • file_contents:
    • a: [0,1,1,1,0]
    • b: [0,0,0,1,1]

I am trying to find out all the documents that have a particular file_path and that have file_contents.a in them.

My query at the moment looks like this:

db.logs.find({$and:[{'file_path':"/username/innerdirectory/file.txt"},{file_contents.a:{$exists: true}}]})

It says SyntaxError: missing : after property id

Is my query even right?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
omrakhur
  • 1,362
  • 2
  • 24
  • 48
  • Try putting quotes on "file_contents.a" – Héctor Oct 27 '17 at 09:36
  • Tried that, doesn't give an error, but gives no result either. Although for my particular file name, the field `file_contents.a` does in fact exist – omrakhur Oct 27 '17 at 09:40
  • Then you should try in an example where some result must be returned... I mean, where `file_contents.a` exists – Héctor Oct 27 '17 at 09:43
  • That's what I tried, it does exist for the document for which I tried it – omrakhur Oct 27 '17 at 09:45
  • FYI. You almost never need `$and` as [ALL MongoDB query operations are already AND conditions](https://docs.mongodb.com/manual/tutorial/query-documents/#specify-and-conditions) "by default". `{ "file_path": "/username/innerdirectory/file.txt", "file_contents.a": { "$exists": true } }` – Neil Lunn Oct 27 '17 at 09:49
  • @NeilLunn But with $and it should work, right? – Héctor Oct 27 '17 at 09:54
  • 1
    @Héctor Yes, however it's really superfluous and overly terse. The whole point on any operator in the DSL which uses an `Array` as an argument is to "avoid key name collision". 9/10 times even multiple conditions simply assign to the same key. i.e `{ "value": { "$gt": 1, "$lt": 3 } }` is actually an AND expression on the same key. Not the point anyway. The issue is with the "dot notation" being required to be quoted. – Neil Lunn Oct 27 '17 at 09:58

0 Answers0