简体   繁体   English

使用 nodejs 和 mongoose 从 mongodb 中具有 id 的对象数组中获取元素

[英]Get an element from an array of objects with an id in mongodb using nodejs and mongoose

I've a many documents like this我有很多这样的文件

user:62e13ae4f9a38f7610e70bd7,
_id :62e13ae4f9a38f7610e70bdb
transactions:{

 {
            "amount": 50,
            "category": "Bills",
            "type": "Expense",
            "date": "2022-01-20T00:00:00.000Z",
            "_id": "62e13ae4f9a38f7610e70be0"
        },
        {
            "amount": 100,
            "category": "Lottery",
            "type": "Income",
            "date": "2022-01-20T00:00:00.000Z",
            "_id": "62e13ae4f9a38f7610e70be1"
        },
        {
            "amount": 200,
            "category": "Salary",
            "type": "Income",
            "date": "2022-01-20T00:00:00.000Z",
            "_id": "62e13ae4f9a38f7610e70be2"
        }
}


And I want to retrieve an object from the transactions array with a particular id (I only want that object )我想从具有特定 id 的事务数组中检索一个 object (我只想要那个 object )

I tried some methods like我尝试了一些方法,例如

      const transactions = await Transactions.find({
        user: req.user._id,
        "transactions._id": {$eq: req.params._id },
      });

  const transactions = await Transactions.find({
        user: req.user._id,
        "transactions": { _id: req.params._id },
      });
            const transactions = await Transactions.find({
        user: req.user._id,
        "transactions": { $elemMatch:{_id: req.params._id }},
      });

but nothing seems to be working, can anyone help me to solve this And please mention the mistake I made.但似乎没有任何效果,任何人都可以帮我解决这个问题并请提及我犯的错误。

Try to match the transactions._id directly:尝试直接匹配transactions._id

"transactions._id": req.params._id

Example例子

const transactions = await Transactions.find({
  user: req.user._id,
  "transactions._id": req.params._id
});

Update更新

From the comment, it's possible to use projection as the second parameter of .find() to return only the object it found in the transactions .从评论中,可以使用投影作为.find()的第二个参数来仅返回它在transactions中找到的 object 。

const transactions = await Transactions.find({
  user: req.user._id,
  "transactions._id": req.params._id
}, { "transactions.$": 1 });

More information 更多信息

If you only want that matching element, you have to use aggreation.如果你只想要那个匹配的元素,你必须使用聚合。

db.collection.aggregate([
{
  $unwind: "$transactions"
},
{
  $match: {
    "transactions.id": "idGoesHere"
  }
}
])

As you commented in the other answer, you could use positional operator to project the matching elements as well.正如您在另一个答案中评论的那样,您也可以使用positional运算符来投影匹配的元素。

暂无
暂无

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

相关问题 如何在Mongoose MongoDb NodeJS中的数组中获取集合中的连接对象 - How to get the connected objects within a collection in an array in Mongoose MongoDb NodeJS 从ID中获取与对象子数组与NodeJS中的猫鼬匹配的父对象 - Get parent object from id that matches objects child array with mongoose in NodeJS 如何使用 express nodejs 从猫鼬中的 Id 数组中获取所有对象? - How get all objects from an array of Id's in mongoose with express nodejs? 如何使用nodejs删除mongoose对象数组中的特定元素? - How to remove the specific element in an array of objects in mongoose using nodejs? MongoDB 数组对象中的聚合搜索 [nodejs, mongoose] - MongoDB aggregation search in objects of array [nodejs, mongoose] 如何从Mongodb到Mongoose的嵌套文档中获取Nodejs中的数组? - How to get an Array in Nodejs from a nested document in Mongodb through Mongoose? 如何在nodejs中使用mongoose将对象数组和嵌套对象数组插入mongodb - how to insert array of objects and array of nested objects into mongodb using mongoose in nodejs 从 mongodb 集合 mongoose nodejs 中删除一个元素 - remove an element from mongodb collection mongoose nodejs 使用 nodejs 和 mongoose 从具有动态嵌套级别的 mongodb 获取数据 - Get data from mongodb with dynamic nested levels using nodejs and mongoose 如何使用 mongoose、nodejs 从 Mongodb 获取数据 - How can i get data from Mongodb using mongoose, nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM