sayres
sayres

Reputation: 381

MongoDB get value of document's Item

this is my schema document in MongoDB:

{
"_id" : UUID("236073ce-a583-4df4-ba7d-bda6db186d10"),
"Lat" : "",
"Lng" : "",
"CreationDateTime" : ISODate("2017-09-26T06:39:29.105Z"),
"DeviceId" : "89984320001499681815",
"Topic" : "",
"UserId" : UUID("bca0db12-2246-49a5-8703-b03fee45e50f"),
"UserName" : "",
"Data" : {
    "AppVersion" : "",
    "AppName" : ""
},
"DeviceIdId" : ,
"FirstName" : " ",
"LastName" : "",
"AllowDomains" : "",
"JobLocationName" : ""
}

How could I get just DeviceId as a string?

I tried this :

var result;
db.getCollection('FinalLocation').find({}).forEach(function(u){
result = u.DeviceId;
});

but it is wrong.

Upvotes: 0

Views: 124

Answers (2)

Mithun
Mithun

Reputation: 153

Please use the following code (if you want to display in robomongo)

        var result;
        db.getCollection('FinalLocation').find({}, {DeviceId: 1}).forEach(function(u){
        result = u.DeviceId;
        print(result);
        });

Upvotes: 1

Evgeny Melnikov
Evgeny Melnikov

Reputation: 1092

Use this (if i understood your question correctly):

var result;
db.getCollection('FinalLocation').find({}, {DeviceId: 1}).forEach(function(u){
result = u.DeviceId;
});

It will work well)

https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/

Upvotes: 0

Related Questions