mongodb-labs / mongorover

Intern project - MongoDB driver for the Lua programming language - This Repository is NOT a supported MongoDB product
Apache License 2.0
55 stars 10 forks source link

How to do $elemMatch query on mongorover #45

Closed priyaradhakrishnan0 closed 8 years ago

priyaradhakrishnan0 commented 8 years ago

The collection anchors has the structure {"anchor":some_anchor , pages ;[ { "page_freq" : some_number, "page_id" : some_number }]}

I want to query anchors db for pages with page_id = 321544

In mongo command prompt I give

db.anchors.find({pages: { $elemMatch :{ "page_id":321544} }})

With the mongorover, the following lua code gives exception

local client = mongorover.MongoClient.new("mongodb://"..dbIP..":"..dbPort) AnchorDb = client:getDatabase("anchorDB") anchors = AnchorDb:getCollection("anchors") local results = anchors:find({pages.page_id = "321544"})

Error is '}' expected near '='

I am not sure how to specify "pages: { $elemMatch :{ "page_id":321544} }}" in mongorover. Please help.

christopherjwang commented 8 years ago

With the mongorover, the following lua code gives exception...

whenever you have weird "keys" in lua, you put it in square brackets.

local results = anchors:find({["pages.page_id"] = "321544"})

I am not sure how to specify "pages: { $elemMatch :{ "page_id":321544} }}" in mongorover. Please help.

Try this:

local results = anchors:find({pages = {["$elemMatch"] = {page_id = 321544}}})

While this isn't necessarily mongorover related, it is an interesting syntax of lua that really only comes out while writing mongodb queries. Let me know if it helps!

priyaradhakrishnan0 commented 8 years ago

Hi,

Thanks for the prompt reply. It works!!

local results = anchors:find({["pages.page_id"] = 321544})

I have one more request. Can you add an example of

mongorover.MongoDatabase:command (command[, value], options)

in the documentation.

Thanks again,

Priya

On Wed, Jul 13, 2016 at 11:08 PM, Christopher Wang <notifications@github.com

wrote:

With the mongorover, the following lua code gives exception...

whenever you have weird "keys" in lua, you put it in square brackets.

local results = anchors:find({["pages.page_id"] = "321544"})

I am not sure how to specify "pages: { $elemMatch :{ "page_id":321544} }}" in mongorover. Please help.

Try this:

local results = anchors:find({pages = {["$elemMatch"] = {page_id = 321544}}})

While this isn't necessarily mongorover related, it is an interesting syntax of lua that really only comes out while writing mongodb queries. Let me know if it helps!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mongodb-labs/mongorover/issues/45#issuecomment-232430640, or mute the thread https://github.com/notifications/unsubscribe/AFs7Y-vtExg2BETIV2Xh-m2ZwHOLGEAOks5qVSKzgaJpZM4JLQlz .

christopherjwang commented 8 years ago

@priyaradhakrishnan0 https://github.com/mongodb-labs/mongorover/blob/master/test/TestDatabase.lua#L80

priyaradhakrishnan0 commented 8 years ago

Thanks a lot. -priya