Er. Mohit Agrawal
Er. Mohit Agrawal

Reputation: 2276

How to fetch specific fields in Mongo DB only values without keys?

My data set is following:

{ "_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" : { "@@@and@@@" : [ { "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" : { "@@@and@@@" : [ { "email" : { "eq" : "" } } ] } }, "updated_on" : ISODate("2016-01-13T10:31:13.897Z"), "created_on" : ISODate("2016-01-13T10:31:13.897Z") }

and i am running following query

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

but it give me

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

but what i want is

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

Upvotes: 1

Views: 129

Answers (2)

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

You can use aggregation to achieve what you want. You can try this out

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

Upvotes: 0

chridam
chridam

Reputation: 103365

Perform a distinct() query against the _id field on the collection:

db.eventqueries.distinct("_id")

In the node.js driver run the distinct() command which would have a callback with the results:

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

Upvotes: 1

Related Questions