2016-01-13 54 views
1

我的數據集如下:如何獲取MongoDB中的特定字段只有沒有鍵的值?

{ "_id" : "EVNQUERY-k9Uoo74Q3WcdmU", "event_data" : "{\"data\":\"helo\"}", "event_name" : "onOemPushAlert", "timestamp_start" : 1452678481433, "timestamp_end" : 1452678481433, "sender" : "USR-ADMIN", "status" : "pending", "query" : { "user" : { "and" : [ { "email" : { "eq" : "" } } ] } }, "updated_on" : ISODate("2016-01-13T09:48:01.459Z"), "created_on" : ISODate("2016-01-13T09:48:01.459Z") } 

{ "_id" : "EVNQUERY-yAKmrbzs2JeaUY", "event_data" : "{\"data\":\"ji\"}", "event_name" : "onOemPushAlert", "timestamp_start" : 1452681060159, "timestamp_end" : 1452681060159, "sender" : "USR-ADMIN", "status" : "pending", "query" : { "user" : { "@@@[email protected]@@" : [ { "email" : { "eq" : "" } } ] } }, "updated_on" : ISODate("2016-01-13T10:31:00.223Z"), "created_on" : ISODate("2016-01-13T10:31:00.223Z") } 

{ "_id" : "EVNQUERY-0NeCstt2eJchDZ", "event_data" : "{\"data\":\"hyg\"}", "event_name" : "onOemPushAlert", "timestamp_start" : 1452681073882, "timestamp_end" : 1452681073882, "sender" : "USR-ADMIN", "status" : "pending", "query" : { "user" : { "@@@[email protected]@@" : [ { "email" : { "eq" : "" } } ] } }, "updated_on" : ISODate("2016-01-13T10:31:13.897Z"), "created_on" : ISODate("2016-01-13T10:31:13.897Z") } 

和我運行下面的查詢

db.eventqueries.find({},{_id:1}) 

,但它給我

{ "_id" : "EVNQUERY-k9Uoo74Q3WcdmU" } 
{ "_id" : "EVNQUERY-yAKmrbzs2JeaUY" } 
{ "_id" : "EVNQUERY-0NeCstt2eJchDZ" } 

,但我想要的是

["EVNQUERY-k9Uoo74Q3WcdmU", "EVNQUERY-yAKmrbzs2JeaUY", "EVNQUERY-0NeCstt2eJchDZ"] 

回答

1

在收集執行鍼對_id領域distinct()查詢:

db.eventqueries.distinct("_id") 

在node.js的驅動程序運行distinct()命令這將有結果的回調:

collection.distinct("_id", function(err, docs) { 
    if (err) { /* handle error */ }; 
    console.log(docs); 
}); 
0

你可以使用聚合來實現你想要的。你可以試試這個

db.eventqueries.aggregate([ 
    { 
     $group : { 
      keys : { $push : "_id" } 
     } 
    } 
]) 
相關問題