【问题标题】:How to extract unique data on basis of combinations of fields如何根据字段组合提取唯一数据
【发布时间】:2023-03-06 08:45:01
【问题描述】:

我的架构是

var FlatSchema = new Schema({
    tower: { type: Schema.ObjectId, ref: "Tower" },
    project: { type: Schema.ObjectId, ref: "Project" },
    status: String,
    floor: Number,
    size: String,
    type: String,
    flat_number: String,
    price: Number,
    price_per_unit: Number,
    carpetArea: Number,
    directionFacing: String,
    livingRoomArea: Number,
    kitchenArea: Number,
    balconies: Number,
    bathRooms: Number,
    furnishingState: String,
    flooringType: String,
    FloorPlans_2d: String,
    FloorPlans_3d: String,
    createdAt: { type: Date, 'default': Date.now },
    isDeleted: { type: Boolean, 'default': false }
});

我想获取项目 id 等于给定 id 的所有公寓,并且结果在地毯面积和类型的基础上是唯一的。

Flat.find({ project: req.params.project, isDeleted: false}, function(err, flats) {
        if (err) { return res.status(500).send(err); }
        if (!flats) { return res.json(401); }
        res.status(200).json(flats);
    });

您能否编辑我的查询以获得所需的结果?

【问题讨论】:

  • 你试过什么?唯一的查询尝试是非常简单的,与您的要求完全不同。请展示一个真正的尝试。不是代码编写服务。
  • 我不知道,所以我贴了这个问题
  • 这是一个非常简单的查找。在我能想到的每一次搜索中,我想到的关于您所要求的内容的即时关键词都会返回相关结果。您不能只是在这里发帖并要求人们为您编写代码。不幸的是,您的问题在描述您期望的输出时也非常模糊。事实上过于模糊,无法保证收到的赞成票。

标签: node.js mongodb mongoose


【解决方案1】:

获得carpetArea 和type 的独特基础的一种方法是通过aggregation,示例代码是

Flat.aggregate([
      {$match: { project: req.params.project, isDeleted: false}},
      {$group: {_id: {carpetArea: '$carpetArea', type: '$type'},
                status: {$first: '$status'},
                // other field are here same type above
               }}
      ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多