0

Here is the sample collections for the query, I want the result like given below. How can I show the details of device and user's details in issuedDevices collection's document using mongo/nodejs query, please help:

1. collection user

/* 0 */
{
    "_id" : 1,
    "name" : "Pradeep",
    "email" : "abc@gmail.com",
    "createDate" : ISODate("2015-05-23T00:03:32.350-06:30")
}

/* 1 */
{
    "_id" : 2,
    "name" : "Steve",
    "email" : "xyz@gmail.com",
    "createDate" : ISODate("2015-05-23T00:04:02.362-06:30")
}

/* 2 */
{
    "_id" : 3,
    "name" : "Jonhy",
    "email" : "xyz123@gmail.com",
    "createDate" : ISODate("2015-05-23T00:04:30.834-06:30")
}

/* 3 */
{
    "_id" : 4,
    "name" : "Pinty",
    "email" : "p123@gmail.com",
    "createDate" : ISODate("2015-05-23T00:05:11.035-06:30")
}

2. collection Devices:

/* 0 */
{
    "_id" : 1,
    "name" : "iPad",
    "owner" : "james"
}

/* 1 */
{
    "_id" : 2,
    "name" : "iPhone",
    "owner" : "Micheal"
}

3. collection issuedDevices:

 /* 0 */
    {
        "_id" : ObjectId("55602058a64447a5071a9a85"),
        "userId" : 1,
        "deviceId" : 1,
        "isReturnd" : false,
        "issuedDateTime" : ISODate("2015-05-23T00:08:16.354-06:30")
    }

/* 1 */
{
    "_id" : ObjectId("55602065a64447a5071a9a86"),
    "userId" : 2,
    "deviceId" : 1,
    "isReturnd" : true,
    "issuedDateTime" : ISODate("2015-05-23T00:08:29.355-06:30")
}

/* 2 */
{
    "_id" : ObjectId("5560207da64447a5071a9a87"),
    "userId" : 3,
    "deviceId" : 1,
    "isReturnd" : true,
    "issuedDateTime" : ISODate("2015-05-23T00:08:53.615-06:30")
}

I want the result like given below, how can I show the details of device and user's details in issuedDevices collection's document using mongo/nodejs query.

Expected Output

 [
     {
        "_id" : ObjectId("5560207da64447a5071a9a87"),
        "userId" : 3,
        "deviceId" : 1,
        "userDetails":{
            "_id" : 3,
            "name" : "Jonhy",
            "email" : "xyz123@gmail.com",
            "createDate" : ISODate("2015-05-23T00:04:30.834-06:30")
        }
        "deviceDetails":{
            "_id" : 2,
            "name" : "iPhone",
            "owner" : "Micheal"
        },
        "isReturnd" : true,
        "issuedDateTime" : ISODate("2015-05-23T00:08:53.615-06:30")
    }
]
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • possible duplicate of [How do I perform the SQL Join equivalent in MongoDB?](http://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb) – Sylvain Leroux May 23 '15 at 11:36

1 Answers1

1

The mongodb query that would produce the desired result would use the forEach() method of the find() cursor on db.issuedDevices to iterate over the documents and create a new document with the results which is then saved to same collection. The following demonstrates this for user with id 3:

db.issuedDevices.find({"userId" : 3}).forEach(function (doc){
      var userDetails = db.user.findOne({"_id": 3});
      var deviceDetails = db.devices.findOne({"_id": doc.deviceId});
      doc.userDetails = userDetails;
      doc.deviceDetails = deviceDetails;
      db.issuedDevices.save(doc);
})

Querying the issuedDevices collection db.issuedDevices.find({"userId" : 3}) would yield:

/* 0 */
{
    "_id" : ObjectId("5560207da64447a5071a9a87"),
    "userId" : 3,
    "deviceId" : 1,
    "isReturnd" : true,
    "issuedDateTime" : ISODate("2015-05-23T06:38:53.615Z"),
    "userDetails" : {
        "_id" : 3,
        "name" : "Jonhy",
        "email" : "xyz123@gmail.com",
        "createDate" : ISODate("2015-05-23T06:34:30.834Z")
    },
    "deviceDetails" : {
        "_id" : 1,
        "name" : "iPad",
        "owner" : "james"
    }
}

In Mongoose, this can be done by making nested calls within IssuedDevice model but using lean() as documents returned from queries with the lean option enabled are plain javascript objects that you can then manipulate, not MongooseDocuments :

IssuedDevice.findOne({"userId" : 3}).lean().exec(function (err, doc) {
    User.findOne({"_id": 3}).exec(function (err, user) {
        Devices.findOne({"_id": doc.deviceId}).exec(function (err, device) {
            doc.userDetails = user;
            doc.deviceDetails = device;
            console.log(doc); // <--- gives you the result
        });
    });          
});
chridam
  • 100,957
  • 23
  • 236
  • 235