猫鼬填充和联接集合上的where子句

时间:2019-05-07 05:53:32

标签: javascript mongoose mongoose-populate

我正在尝试使用猫鼬使用where子句进行多联接。我该如何正确地执行where子句?

我查看了猫鼬人口文档和其他答案,但是由于当前查询,其他文档又回来了。我有一个正在运行的节点快递服务器,并且正在使用Postman命中端点。

这是我的主要架构,具有所有外键。储物柜模式中有一个名为IsUsed(布尔)的属性,我正在尝试将其用于我的where子句

const userLockerMapSchema = new Schema({
  UserId: { type: Schema.Types.ObjectId, ref: "User" },
  SchoolId: { type: Schema.Types.ObjectId, ref: "School" },
  StationId: { type: Schema.Types.ObjectId, ref: "Station" },
  LockerId: { type: Schema.Types.ObjectId, ref: "Locker" },
});

要加入这些集合,我正在执行以下查询:

UserLockerMap.find({
      UserId: userId
    })
      .populate("SchoolId")
      .populate("StationId")
      .populate({
        path: "LockerId",
        match: {
          IsUsed: true
        }
      })

我希望查询仅返回其中LockerId.IsUsed = true的集合列表。像这样:

{
        "_id": "5c39825a9d268184fe5",
        "UserId": "5ca4oiuo23ua30ce528",
        "SchoolId": {
            "EmailDomain": [
                "@tu1"
            ],
            "_id": "5cd2nlkacf0d48e333",
            "Name": "Test Uni 1",
            "CreatedAt": "2019-05-07T00:01:16.506Z",
            "__v": 0
        },
        "StationId": {
            "_id": "5cdlaje93cf0d48e334",
            "AtSchoolId": "5cd0cacc24ae93cf0d48e333",
            "Name": "Test S1",
            "CreatedAt": "2019-05-07T00:01:29.095Z",
            "__v": 0
        },
        "LockerId": {
            "_id": "5cd0caea24ae93cf0d48e337",
            "StationId": "5cd0cad924ae93cf0d48e334",
            "LockerName": "TL2",
            "IsUsed": true,
            "CreatedAt": "2019-05-07T00:01:46.784Z",
            "__v": 0
        },
        "StartedAt": "2019-05-07T05:07:14.161Z",
        "EndAt": "2019-05-07T05:12:14.161Z",
        "__v": 0
    }

上面的查询实际上返回所有集合,但是LockerId值返回为null,其中LockerId.IsUsed!= true。即

{
        "_id": "97287e5b7cab8a0d173cbddb7",
        "UserId": "5ca812232327a30ce528",
        "SchoolId": {
            "EmailDomain": [
                "@tu1"
            ],
            "_id": "9238ojjelkjlka33q",
            "Name": "Test Uni 1",
            "CreatedAt": "2019-05-07T00:01:16.506Z",
            "__v": 0
        },
        "StationId": {
            "_id": "5cd0cad924ae93cf0d48e334",
            "AtSchoolId": "5cd0cacc24ae93cf0d48e333",
            "Name": "Test S1",
            "CreatedAt": "2019-05-07T00:01:29.095Z",
            "__v": 0
        },
        "LockerId": null,
        "StartedAt": "2019-05-07T01:56:07.829Z",
        "EndAt": "2019-05-07T02:01:07.829Z",
        "__v": 0
    },
{
        "_id": "5c39825a9d268184fe5",
        "UserId": "5ca4oiuo23ua30ce528",
        "SchoolId": {
            "EmailDomain": [
                "@tu1"
            ],
            "_id": "5cd2nlkacf0d48e333",
            "Name": "Test Uni 1",
            "CreatedAt": "2019-05-07T00:01:16.506Z",
            "__v": 0
        },
        "StationId": {
            "_id": "5cdlaje93cf0d48e334",
            "AtSchoolId": "5cd0cacc24ae93cf0d48e333",
            "Name": "Test S1",
            "CreatedAt": "2019-05-07T00:01:29.095Z",
            "__v": 0
        },
        "LockerId": {
            "_id": "5cd0caea24ae93cf0d48e337",
            "StationId": "5cd0cad924ae93cf0d48e334",
            "LockerName": "TL2",
            "IsUsed": true,
            "CreatedAt": "2019-05-07T00:01:46.784Z",
            "__v": 0
        },
        "StartedAt": "2019-05-07T05:07:14.161Z",
        "EndAt": "2019-05-07T05:12:14.161Z",
        "__v": 0
    }

0 个答案:

没有答案