如何从其他集合和查询中填充引用的文档?

普拉迪普·古普塔

这是查询的样本集合,我想要下面给出的结果。如何issuedDevices使用mongo / nodejs查询集合的文档中显示设备的详细信息和用户的详细信息,请帮助:

1.收款用户

/* 0 */
{
    "_id" : 1,
    "name" : "Pradeep",
    "email" : "[email protected]",
    "createDate" : ISODate("2015-05-23T00:03:32.350-06:30")
}

/* 1 */
{
    "_id" : 2,
    "name" : "Steve",
    "email" : "[email protected]",
    "createDate" : ISODate("2015-05-23T00:04:02.362-06:30")
}

/* 2 */
{
    "_id" : 3,
    "name" : "Jonhy",
    "email" : "[email protected]",
    "createDate" : ISODate("2015-05-23T00:04:30.834-06:30")
}

/* 3 */
{
    "_id" : 4,
    "name" : "Pinty",
    "email" : "[email protected]",
    "createDate" : ISODate("2015-05-23T00:05:11.035-06:30")
}

2.收集设备:

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

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

3.收集issueDevices:

 /* 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")
}

我想要如下所示的结果,如何issuedDevices使用mongo / nodejs查询集合的文档中显示设备的详细信息和用户的详细信息

预期产量

 [
     {
        "_id" : ObjectId("5560207da64447a5071a9a87"),
        "userId" : 3,
        "deviceId" : 1,
        "userDetails":{
            "_id" : 3,
            "name" : "Jonhy",
            "email" : "[email protected]",
            "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")
    }
]
克列丹

产生期望结果的mongodb查询将使用游标forEach()方法遍历文档,并使用结果创建一个新文档,然后将其保存到同一集合中。以下为ID为3的用户演示了此操作:find()db.issuedDevices

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);
})

查询issueDevices集合db.issuedDevices.find({"userId" : 3})将产生:

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

在Mongoose中,这可以通过在IssuedDevice模型中进行嵌套调用来实现,但是lean()在启用了lean选项的情况下,查询返回的文档用作普通javascript对象,而不是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
        });
    });          
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

缺少类似联接的查询的数据库设计在MongoDB中:如何对其他集合中的文档使用引用

如何从其他集合填充文档。具体案例

MongoDB如果其他集合中存在字段/引用,则从汇总中排除文档

MongoDB Compass-文档未显示其他集合中引用了对象ID(Ref)的字段

如何从集合中与列表字段元素的值匹配的其他集合中获取文档

如何从 Flutter 和 Firestore 中“Y”集合的文档 id 查询“X”集合的文档

如何获取集合中的最新文档并删除其他文档

如何串联F#中的列表(和其他集合)?

在forEach中查询其他集合

如何用猫鼬中另一个集合的文档填充文档的字段时查询文档?

流星:如何自动使用存储在集合中其他字段中的数组长度填充字段?

如何在猫鼬中填充来自其他集合的数据?

如何使用Mongoose将多个引用保存到MongoDB中的其他文档?

如何表达集合中的其他内容?

如何通过引用 BigQuery SQL 中的其他字段值来填充值?

如何正确引用其他JSON模式文档($ ref)

如何在数据库查询填充的选项表中添加其他选项

如何引用其他模块中的值?

如何使用其他工作表中的所有值填充Google文档中的一个工作表?

如何在MongoDB中将属性添加到集合中的所有文档以及其他属性的相同数据?

在MongoDB中快速查询和删除大量集合的文档

如何通过 Fauna 中的集合文档中的数据进行查询?

mongoDB根据条件从其他集合的链接文档中查找带有值的文档

如何在嵌套文档中填充对模型的多个引用?

我如何查询引用结果集中的其他行?

在 mongo 中更新集合文档时,在使用它的所有其他文档或集合中更新它

查询表,同时排除其他表中引用的值

您可以在 Elasticsearch percolator 中引用其他查询吗?

如何使用下拉菜单中的数据和其他两个条件填充表格?