tc / RMongo

R client to interface with MongoDB
102 stars 34 forks source link

Projection problem ? #25

Open Ryo51289 opened 10 years ago

Ryo51289 commented 10 years ago

Here is my data test store in MongoDB

{
        "_id" : ObjectId("52983ff67dbf497a8bb0192b"),
        "data" : [
                {
                        "LoadPct" : 10,
                        "RECORD_SEQ" : 1
                }
        ]
}
{
        "_id" : ObjectId("5298400b7dbf497a8bb0192d"),
        "data" : [
                {
                        "LoadPct" : 59,
                        "RECORD_SEQ" : 2
                }
        ]
}
{
        "_id" : ObjectId("529840217dbf497a8bb01934"),
        "data" : [
                {
                        "LoadPct" : 8,
                        "RECORD_SEQ" : 3
                }
        ]
}
....

I can not perform a query to get data like result of following query when i run it on mongo shell

db.collect.find({'data.LoadPct':{$gt:10}},{'data.LoadPct':1}).forEach(printjson)
{
        "_id" : ObjectId("5298404d7dbf497a8bb01942"),
        "data" : [
                {
                        "LoadPct" : 59
                }
        ]
}
......

My R-Code to get result like above query

data <- dbGetQueryForKeys(mongoConn,"collect","{'data.LoadPct':{'$gt':10}}","{'data.LoadPct':1}")

and data return

 data.LoadPct                     X_id
1           NA 5298400b7dbf497a8bb0192d
2           NA 5298404d7dbf497a8bb01942
3           NA 5298408c7dbf497a8bb019a2
4           NA 529840cc7dbf497a8bb019f5
5           NA 529840d17dbf497a8bb019fc
.....

other code

data <- dbGetQueryForKeys(mongoConn,"collect","{'data.LoadPct':{'$gt':10}}","{'data':1,data.LoadPct:1}")

result

                  data data.LoadPct                     X_id
1 [ { "LoadPct" : 59}]           NA 5298400b7dbf497a8bb0192d
2 [ { "LoadPct" : 24}]           NA 5298404d7dbf497a8bb01942
3 [ { "LoadPct" : 50}]           NA 5298408c7dbf497a8bb019a2
4 [ { "LoadPct" : 22}]           NA 529840cc7dbf497a8bb019f5
5 [ { "LoadPct" : 11}]           NA 529840d17dbf497a8bb019fc
.....