0

Sample json object :

{ "_id" : ObjectId( "55887982498e2bef5a5f96db" ),
  "a" : "x",
  "q" : "null",
  "p" : "",
  "s" : "{\"f\":{\"b\":[\"I\"]},\"time\":\"fs\"}" }

need all documents where time = fs My query :

{"s":{"time" : "fs"}}

above returns zero products but that is not true.

NooB8374
  • 99
  • 12
  • As far as I can "s" contains a simple string so there is no way it query like this will work. – zero323 Jun 23 '15 at 16:59
  • So is it not possible to query on such documents – NooB8374 Jun 23 '15 at 17:00
  • You can always use [`regex`](http://docs.mongodb.org/manual/reference/operator/query/regex/) but it will be rather slow. Is there any particular reason why you keep field as a string and not a nested document? – zero323 Jun 23 '15 at 17:01
  • Nested document would have allowed being query upon ?, any how-to about that ? – NooB8374 Jun 23 '15 at 17:03

1 Answers1

0

There are two problems here. First of all s is clearly a string so your query cannot work. You can use $regex as below but it won't be very efficient:

{s: {$regex: '"time"\:"fs"'}}

I would suggest converting s fields to proper documents. You can use JSON.parse to do it. Documents can be updated based on a current value using db.foo.find().snapshot().forEach. See this answer for details.

Second problem is your query is simply wrong. To match time field you should use dot notation:

{"s.time" : "fs"})
Community
  • 1
  • 1
zero323
  • 322,348
  • 103
  • 959
  • 935