这是我的代码,可以得到一个楼层平面图,并填充所有与此相关的公寓

请参见下面的代码:

var floorplan = Floorplan.find({ 
    project: req.params.project, 
    tower: req.params.tower, 
    isDeleted: false 
});
floorplan.populate('flats').exec(function(err, floorplan) {
    if (err) { return res.send(err); }
    if (!floorplan) { return res.status(401).json(); }
    res.status(200).json(floorplan);
});

但我只想填充那些被删除的单元:false

平面布置方案

var FloorplanSchema = new Schema({
    project: { type: Schema.ObjectId, ref: "Project" },
    flats: [{ type: Schema.ObjectId, ref: "Flat" }],
    tower: [{ type: Schema.ObjectId, ref: "Tower" }],
    unitType: String,
    area: Number,
    floorPlan2D: String,
    floorPlan3D: String,
    livingRoomArea: Number,
    kitchenArea: Number,
    balconies: Number,
    bathRooms: Number,
    isDeleted: { type: Boolean, 'default': false },
    createdAt: { type: Date, 'default': Date.now }
});

平面模式

var FlatSchema = new Schema({
    tower: { type: Schema.ObjectId, ref: "Tower" },
    floorplan: { type: Schema.ObjectId, ref: "Floorplan" },
    project: { type: Schema.ObjectId, ref: "Project" },
    status: String,
    floor: Number,
    size: String,

    superbuiltup_area: Number,

    directionFacing: String,
    furnishingState: String,
    flooringType: String,
    createdAt: { type: Date, 'default': Date.now },
    isDeleted: { type: Boolean, 'default': false },

});

推荐答案

100方法有一个允许过滤的选项,你可以try

Flo或plan
.find({ 
    project: req.params.project, 
    tower: req.params.tower, 
    isDeleted: false 
})
.populate({
    path: 'flats',
    match: { isDeleted: false }
})
.exec(function(err, flo或plan) {
    if (err) { return res.send(err); }
    if (!flo或plan) { return res.status(401).json(); }
    res.status(200).json(flo或plan);
});

Flo或plan
.find({ 
    project: req.params.project, 
    tower: req.params.tower, 
    isDeleted: false 
})
.populate('flats', null, { isDeleted: false })
.exec(function(err, flo或plan) {
    if (err) { return res.send(err); }
    if (!flo或plan) { return res.status(401).json(); }
    res.status(200).json(flo或plan);
});

Mongodb相关问答推荐

为什么这个查询可以在MongoDB中使用?

在MongoDB中获取所有帖子时如何获取相关用户信息

聚合管道可以用于更新数据库中的文档吗?

MongoDB 聚合 groupBy 日期并计算子文档

根据条件删除一些数组元素并将数组的大小更新为mongo中的另一个文件

$group 和 sum + 添加所有大于

使用 $addFields 将字段添加到 $lookup 结果中的每个项目

如何在mongodb中级联删除文档?

如何在 Mongo 聚合管道中获取限制之前的计数

将 MongoDB 地理空间索引与 3d 数据结合使用

删除嵌套文档数组中的嵌入文档

你如何让 mongo 在远程服务器上运行?

MongoDB 1.6.5:如何重命名集合中的字段

使用 Mongoid 和 Ruby 查询最近 30 天的日期范围?

在 Postgres JSON 数组中查询

错误:Could not connect to any servers in your MongoDB Atlas cluster

如何从 Mongoose 模型对象中获取集合名称

MongoDB MapReduce - 发出一个键/一个值不调用reduce

MongoError:Can't extract geo keys from object

我可以只获取 Cursor 对象(pymongo)中的第一项吗?