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

Nested object is converted to an array if it has only one key value pair #34

Closed jestan closed 8 years ago

jestan commented 8 years ago

Lets say, you have following objects in db.products


{"_id" : "1",  "name", "product1", "charging" : { "type" : "free"}}
{"_id" : "2",  "name", "product2", "charging" : { "type" : "flat", "frequency" : "monthly"}}
result = db.products.find_one({ _id = "2"}, { name = 1, ["charging.type"] = 1, ["charging.frequency"] = 1})

result is { _id = "1", name = "prod", charging = {"free"}}

but for following it returns correct value

result = db.products.find_one({ _id = "1"}, { name = 1, ["charging.type"] = 1, ["charging.frequency"] = 1})

result is { _id = "1", name = "prod", charging = { type = "free", frequency = "monthly"}}

jestan commented 8 years ago

Additional Info

lua -e "print(_VERSION)" Lua 5.1

lua -e "print(require('mongorover')._VERSION)" 0.0.1

christopherjwang commented 8 years ago

Hey! First off, sorry for the delayed response. I'm afraid I wasn't watching this repository (I am now though!).

I'm trying to reproduce your code issue on my end.

here's my mongo shell:

> use mongorover_issue_34
switched to db mongorover_issue_34
> db.products.insert({"_id" : "1",  "name": "product1", "charging" : { "type" : "free"}} )
WriteResult({ "nInserted" : 1 })
> db.products.insert({"_id" : "2",  "name": "product2", "charging" : { "type" : "flat", "frequency" : "monthly"}} )
WriteResult({ "nInserted" : 1 })

here's my issue_34.lua script used to try and reproduce your error. print_r is a function you can find here

local MongoClient = require("mongorover.MongoClient")
local client = MongoClient.new("mongodb://localhost:27017/")
results = client.mongorover_issue_34.products:find({})

print "all documents in collection"
for result in results do
    print_r(result)
end

print "\nyour query results"
result = client.mongorover_issue_34.products:find_one({ _id = "2"}, { name = 1, ["charging.type"] = 1, ["charging.frequency"] = 1})
print_r(result)

and my output is (running the above script in command line)

[57] → lua5.1 issue_34.lua
2016/03/20 00:39:46.0052: [20725]:    DEBUG:      cluster: Client initialized in direct mode.
all documents in collection
table: 0x7fa38b600130 {
  [charging] => table: 0x7fa38b600130 {
                  [1] => "free"
                }
  [name] => "product1"
  [_id] => "1"
}

table: 0x7fa38b603630 {
  [charging] => table: 0x7fa38b603630 {
                  [frequency] => "monthly"
                  [type] => "flat"
                }
  [name] => "product2"
  [_id] => "2"
}

your query results
table: 0x7fa38b6048d0 {
  [charging] => table: 0x7fa38b6048d0 {
                  [frequency] => "monthly"
                  [type] => "flat"
                }
  [name] => "product2"
  [_id] => "2"
}

Are you still running into this issue? I'm afraid this is working as intended on my side, and would definitely be caught via the test suite. I made my end as reproducible as possible if you want to try to emulate that and see if it still is an issue. Let me know if it's still a problem and I'll work with you to fix it.

christopherjwang commented 8 years ago

@jestan I misunderstood your problem, and I see what is happening. I think you meant the query should be this (with an _id of 1 instead of a 2).

result = db.products.find_one({ _id = "1"}, { name = 1, ["charging.type"] = 1, ["charging.frequency"] = 1})

It's apparent in my print out above. I'll look into it. Thanks for the bug report.

christopherjwang commented 8 years ago

@jestan pull request sent. Thanks again!

jestan commented 8 years ago

@christopherjwang thanks for the fix.