0

I'd like to confirm that a parser I wrote is working correctly. It takes a JavaScript mongodb command that could be run from the terminal and converts it to a Java object for the MongoDB/Java drivers.

Is the following .toString() result valid?

{ "NumShares " : 1 , "attr4 " : 1 , "symbol" : { "$regex" : "ESLR%"}}

This was converted from the following JavaScript

db.STOCK.find({ "symbol": "ESLR%" }, { "NumShares" : 1, "attr4" : 1 })

And of course, the data as it rests in the collections

{ "_id" : { "$oid" : "538c99e41f12e5a479269ed1"} , "symbol" : "ESLR" , "NumShares" : 3471.0}

Thanks for all your help

Kyte
  • 834
  • 2
  • 12
  • 27

1 Answers1

1

You've combined the query document and the project document in that find() call in to one document. That's probably not what you want. But those documents are just json so you could use any parser to convert those. There's a few gotchas you'd have to deal with around ObjectIDs, dates, DBRefs, and particularly regular expressions but those can be managed without too much trouble by escaping/quoting them before parsing.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • The separate query conditions are represented as documents which are then 'put' into a master query object. The first code block is the result of the toString() of the final query object. Does it look correct? – Kyte Jun 03 '14 at 13:15
  • 1
    To me, it looks wrong to have both docs put in to one, but I'm not sure how your code will use it. But otherwise looks fine. – evanchooly Jun 03 '14 at 13:17