使用ObjectId进行mongodb聚合查找

时间:2018-06-25 15:55:40

标签: mongodb mongodb-query aggregation-framework

我正在为mongodb中的基本查找所困扰。尝试多种组合后,我不知道出了什么问题。我看到有很多关于SO的问题,但是尝试所有答案后都无济于事。

这是我的用户收藏夹

enter image description here

这是我的物品收藏

enter image description here

每个项目都有一个所有者,该所有者映射到用户集合中用户的_id。 对于我的查询,我试图与他们的相应用户一起获取项目。这是查询

   db.items.aggregate([
    {
        "$lookup" : {
          localField : "owner",
          from : "users",
          foreignField : "_id",
          as : "users"
        }
    }
])

它返回此-

enter image description here

我尝试了其他变体,例如

db.items.aggregate([
    {
        "$lookup" : {
          localField : "owner.str",
          from : "users",
          foreignField : "_id.str",
          as : "users"
        }
    }
])

这将导致用户阵列(所有用户!)的填充不正确。

enter image description here

我在做什么错了?

编辑

这是项目集合

{
  "_id": {
    "$oid": "5b268395c176db1548bd92c2"
  },
  "title": "Item #1",
  "description": "Item 1 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "borrowed",
  "image": "http://cdn.site.com/testimage.png",
  "borrower": "5b2684a0c176db1548bd92d5",
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5aecc8012d3d947ba130800d"
    }
  }
}

{
  "_id": {
    "$oid": "5b2c2c4d3c70af2da07d1266"
  },
  "title": "Item 2",
  "description": "Item 2 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "Available",
  "image": "http://cdn.site.com/testimage1.png",
  "borrower": null,
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5b2684a0c176db1548bd92d5"
    }
  }
}

这是用户集合:

{
  "_id": {
    "$oid": "5aecc8012d3d947ba130800d"
  },
  "email": "n.y@gmail.com",
  "registeredOn": "20/10/2018 12:12:29 AM",
  "avatarURL": "http://gravtar.com/12334",
  "friends": []
}

{
  "_id": {
    "$oid": "5b2684a0c176db1548bd92d5"
  },
  "email": "j.v@gmail.com",
  "registeredOn": "20/10/2018 12:12:29 AM",
  "avatarURL": "http://gravtar.com/12334",
  "friends": [],
  "wishlist": [
    {
      "$ref": "items",
      "$id": {
        "$oid": "5b2831543c70af2da07d11da"
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:3)

var array=[{"name":"May","data1":"1121.0"}]; array.forEach(data => { for(let key in data) Number.isNaN(+data[key]) || (data[key] = +data[key]) }); console.log(array);localField应该具有相同的数据类型。您分别是foreignFieldDBRef

答案 1 :(得分:0)

如果您想用相应的用户填充items记录,我认为您可以在猫鼬populate API中使用find选项

示例代码:

this.read = function(query, populate, onSuccess, onFailure, forcePull, sortQuery, ttl, limitValue, selectQuery) {
        var scope = this;
        logger.info(this.TAG, "Read operation execution started on ORM Object: "
                    + modelName + " with query: " + JSON.stringify(query));
        if (query) {

            var queryObject = this._model.find(query);
            if (populate) {
              queryObject = queryObject.populate(populate);
            }
            if (selectQuery) {
              queryObject = queryObject.select(selectQuery);
            }
            if (sortQuery) {
              queryObject = queryObject.sort(sortQuery);
            }
            if (limitValue) {
              queryObject = queryObject.limit(limitValue);
            }
            queryObject.exec(function(error, records) {
                if (error) {
                    logger.error(scope.TAG, "Error while ORM read: " + JSON.stringify(error));
                    onFailure(error);
                    return;
                }
                logger.info(scope.TAG, "Record read successfully: " + JSON.stringify(records));
                onSuccess(records);
            });
          }
     }

在您的情况下,您可以将填充填充为字符串数组,例如['owner']