简体   繁体   English

当我不知道文档 ID 时,如何使用 mongoose 查询子文档

[英]how can I use mongoose to query a subdocument when I don't know the document Id

I have been tasked with rebuilding a more monolithic database using mongo db.我的任务是使用 mongo db 重建一个更加单一的数据库。 In this database, I have a product document which looks like this.在这个数据库中,我有一个看起来像这样的产品文档。

{
  product_id: 4.
  questions: [
    {
      question_id: 10,
      question_text: "hello?",
      helpfulness: 0
    },
    {
      question_id: 11,
      question_text: "goodbye?",
      helpfulness: 1
    }
  ]
}

I will receive a request to my server that includes the question_id (ie 11) and I will need to increment the question's helpfulness.我将收到对我的服务器的请求,其中包含 question_id(即 11),我需要增加问题的有用性。 I have looked everywhere, but I cant seem to find a method to query my database with only the question id and update that question without first knowing the product Id.我到处找,但我似乎无法找到一种方法来查询我的数据库,只使用问题 ID 并在不知道产品 ID 的情况下更新该问题。 Is there a way to do this?有没有办法做到这一点? thanks so much for any help!非常感谢您的帮助!

I think that given query can help you to fetch and update your product document :我认为给定的查询可以帮助您获取和更新您的产品文档:

db.product.update({questions: {$elemMatch: {question_id:'11'}}},{
     $inc: {"questions.helpfulness": 1}
})

Here $inc is used to increment your helpfulness value by 1这里 $inc 用于将您的帮助值增加 1

Here you go, first you need to match the question id and then inc the relative helpfulness by 1. Do try this out给你,首先你需要匹配问题 id,然后加上 1 的相对帮助。试试这个

db.collection.updateOne({'questions.question_id' : 11}, {$inc : {"questions.$.helpfulness" : 1}})

Output :输出 :

{ 
    "_id" : ObjectId("5f891ca72857d128c68bf01a"), 
    "product_id" : 4.0, 
    "questions" : [
        {
            "question_id" : 10.0, 
            "question_text" : "hello?", 
            "helpfulness" : 0.0
        }, 
        {
            "question_id" : 11.0, 
            "question_text" : "goodbye?", 
            "helpfulness" : 2.0
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法填充 mongoose 深层子文档 - i can't populate mongoose deep subdocument 我怎样才能绕过这个表格? 如果我不知道密码,有没有办法登录任何 ID? - how can i bypass this form? Is there a way to login into any id if i don't know the password? 数据添加到firestore的时候,想用onSnapshot监听,这样就可以自动调用数据了,但是不知道怎么用 - When data is added to the firestore, I want to listen with onSnapshot so that the data can be called automatically, but I don't know how to use 更新期间如何在 mongoose 中找到推送的子文档 ID? - How can I find pushed subdocument ids in mongoose during update? 如何在实例方法中更新Mongoose子文档? - How can I update a Mongoose subdocument in an instance method? 如何将猫鼬子文档分成单独的文件? - How can I separate mongoose subdocument into seperate files? 如何在mongodb(mongoose)中覆盖子文档的数组属性 - How can I overwrite array property of subdocument in mongodb (mongoose) 如果我不知道其ID如何选择元素 - How to select element if I don't know its id 我不明白如何使用document.getElementById('id')。onclick - I don't understand how to use document.getElementById(‘id’).onclick 我不明白如何在猫鼬中使用 findOrCreate 方法 - I don't understand how to use the findOrCreate method with mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM