【问题标题】:How to compare 2 collections and fetch matched field data in mongodb?如何比较 2 个集合并在 mongodb 中获取匹配的字段数据?
【发布时间】:2018-08-22 14:22:23
【问题描述】:

我有两个集合,如下所示。

集合 1:列表

{
"_id" : ObjectId("5b618589d6edcc135c1aee0a"),
"name" : "ABC",
"special" : "Golmal",
"dim" : "2122",
}

集合 2:数据

文档 1:

{
"_id" : ObjectId("5b6023d9589ff6bfb5dd75d7"),
"date" : "2018/08/13",
"special" : "Golmal",
}

文档 2:

{
"_id" : ObjectId("5b602447589ff6bfb5dd7606"),
"date" : "2018/08/13",
"special" : "Godman",
}

在这里我想比较两个集合,如果集合 1 的 "special":"Golmal" 存在于集合 2 文档中,我想显示/获取集合 1 文档中的所有相关日期。

我试过aggregate & $lookup 如下

getAvailableList(): Promise<any> {
    return this._mongoUtility.Db
        .then(db => {
            let list= db.collection('list');
            let data= db.collection('data');           
            list.aggregate([{$lookup:
                {
                    from: data,
                    localField: "special",
                    foreignField: "special",
                    as: "matches"
                }
            },
                {
                    $match: { "matches": { $ne: [{}] } }
                }
            ]);
            return Promise.resolve();
        })
        .catch(err => {
            return Promise.reject({message: 'Not found', err: err});
        });
}

但我得到了空值。

我也使用.find & forEach 绑定如下

 let abc = [];
            list.find({}).toArray()
                .then(arr => {
                    arr.forEach(obj1 => {
                        data.find({special: obj1.special}).toArray()
                            .then(secondArr => {
                                console.log(secondArr);
                                if (secondArr.length > 0) {
                                    abc.push(obj1);
                                }
                            });
                    });
                });
            return Promise.resolve();

但没有结果。我需要 JSON 格式的值。由于 MongoDB 没有连接。有什么办法破解吗?

【问题讨论】:

  • localField 和 foreignField 应该是两个集合的 special
  • 那是个错误。我更新了。

标签: arrays json node.js mongodb


【解决方案1】:

嗯,我改变了我的查询方式,不使用forEach,它对我有用。

我在 mongoDB 网站的 find 中遇到了 $in。这是答案

    return new Promise((res, rej) => {
        this._mongoUtility.Db
            .then(db => {
                let list= db.collection('List');
                let data= db.collection('Data');

                data.find({}).toArray()
                    .then(arr => {
                        let maps = arr.map(data => data.special);
                        list.find({special: {$in: maps}}).toArray()
                            .then(secondArr => {
                                res(secondArr);
                            });
                    });
            })
            .catch(err => {
                return rej({message: 'Not found', err: err});
            });
    });

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我没有使用JOINS。我得到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2021-12-11
    • 2012-02-08
    • 2020-03-03
    相关资源
    最近更新 更多