首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mongoose中获取populate()中的值?

问如何在mongoose中获取populate()中的值?
EN

Stack Overflow用户
提问于 2020-12-14 14:20:39
回答 1查看 200关注 0票数 0

我想返回数组的一个索引对象,但是当我查询时,它返回所有的文档。

这是我的架构(UserTb)

代码语言:javascript
复制
const userTbSchema = new Schema({
  _id: mongoose.Schema.Types.ObjectId,
  userId: String,
  gender: String
}

这是我的另一个Schema(产品)

代码语言:javascript
复制
const productSchema = new Schema({
  _id: mongoose.Schema.Types.ObjectId,
  product: String,
  userTbId: { type: mongoose.Schema.Types.ObjectId, ref: "userTb", required: true },
}

它们在另一个js文件中。这是我的Schema(product)的查询结果,不使用Populate。

代码语言:javascript
复制
{
    "_id": "5fc4c13f32ab3174acb01234",
    "product": "watch",
    "userTbId": "5fc4c13f32ab3174acb08540"
},
{
    "_id": "5fc4c2d6b3a2a054d4dc1235",
    "userId": "car",
    "userTbId": "5fc4c2d6b3a2a054d4dc462c"
}

这是当我使用Populate时查询我的Schema(product)的结果。

代码语言:javascript
复制
{
    "_id": "5fc4c13f32ab3174acb01234",
    "product": "watch",
    "userTbId": {
        "_id": "5fc4c13f32ab3174acb08540",
        "userId": "go05111",
        "gender": "male"
    }
},
{
    "_id": "5fc4c2d6b3a2a054d4dc1235",
    "userId": "car",
    "userTbId": {
        "_id": "5fc4c2d6b3a2a054d4dc462c",
        "userId": "Chips",
        "gender": "female"
    }   
}

我想在proudct中获得唯一的{ "gender“:”男性“}对象,比如...

代码语言:javascript
复制
{
    "_id": "5fc4c13f32ab3174acb01234",
    "product": "watch",
    "userTbId": {
        "_id": "5fc4c13f32ab3174acb08540",
        "userId": "go05111",
        "gender": "male"
    }
}

所以我像这样查询

代码语言:javascript
复制
router.get('/:gender', (req, res, next) => {
    Product.find({
        "userTbId.gender": req.params.gender
    })
    .populate({
        path: 'userTbId',
        match: {
            userId: req.params.userId
        }
    })
    .exec()
    .then(docs => {
        res.status(200).json({
            docs
        }); 
    })
    .catch(err => {
        res.status(500).json({
            error: err
        });
    });
});

但是结果是什么也没有返回。

我认为这个填充是ObjectId,所以即使使用了userTbId (),当使用find()时,值也不会出现。

我尝试了几种不同的方法,但都不是我想要的效果。

我如何在mongoose中修复它?

你能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-14 15:52:41

在mongoose中,populate将在find查询的结果集上执行。在您的产品模式中,userTbId是一个对象id,除非填充它,否则它不会有gender属性。为了仅通过使用mongoose来实现您的目标,您可以编写如下查询。首先,你需要找到你的性别和userId的所有userTbIds。然后查询您的产品模式,以获得返回的userTbIds。欢迎修改和优化。

​

代码语言:javascript
复制
router.get('/:gender', (req, res, next) => {
    userTbId.find({
      "gender": req.params.gender,
      "userId": req.params.userId
    }).exec()
    .then(docs => {
        let ids = docs.map(doc => doc.id);
        Product.find({
            "userTbId": {$in:ids}
        })
        .populate({
            path: 'userTbId'
        })
        .exec()
        .then(docs => {
            res.status(200).json({
                docs
            }); 
        })
        .catch(err => {
            res.status(500).json({
                error: err
            });
        });
    })
    .catch(err => {
        res.status(500).json({
            error: err
        });
    });
});

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65284492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档