简体   繁体   English

如何通过 MongoDB (Mongoose) 中的“_id”字段查找文档的数组项?

[英]How to find document's array item by its "_id" field in MongoDB (Mongoose)?

I have the following schema (NestJS + Mongoose):我有以下架构(NestJS + Mongoose):

@Schema({ timestamps: true })
export class Listing {
  @Prop({
        type: [{
            bidderId: { type: Types.ObjectId, required: true, select: false, ref: User.name, index: true },
            amount: { type: Number, required: true },
            date: { type: Date, required: true }
        }],
        select: false
    })
        bids!: Bid[]
}

so basically every listing document has an array of bids .所以基本上每份listing文件都有一系列的bids
now I notice that automatically mongoDB (or mongoose) creates _id field for every bid item I put into the bids array.现在我注意到自动 mongoDB(或猫鼬)为我放入bids数组的每个投标项目创建_id字段。

My question is, If I have a bid's _id , how can I query it's item from the listing's bids array?我的问题是,如果我有一个出价的_id ,我如何从列表的出价数组中查询它的项目? something like:就像是:

  // Adding a new bid to the listing, and retrieving the updated listing
  const listingWithAddedBid = await this.listingModel.findByIdAndUpdate(listingId, {
    $push: {
        bids: {
            $each: [{
                amount: bidInfo.amount,
                bidderId: new Types.ObjectId(user.id),
                date: new Date()
            }],
            $sort: { amount: -1 }
        }
    }
}, { new: true })

  // Getting the new bid's _id from the array (it will be at index 0 because we sort buy amount)
  const newBidId = listingWithAddedBid.bids[0]._id

  // here how can I query the entire bid item from the array with 'newBidId'? this won't work
  this.listingModel.findById(newBidId) // returns null
this.listingModel.findOne({ "bids._id": newBidId, {}, {}, (error, doc) =>
{
 if (error) {
        ....
      }
      if (doc) {
        ....
      } else {
        ...//'Not Found';
      }
});

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

相关问题 如何查找某个项目是否存在于文档 mongoose 的数组中 - How to find if an item exists in an array of a document mongoose Mongodb mongoose - 如果字段不存在,如何将文档添加到数组 - Mongodb mongoose - How to add a document to an array if not present by a field 如何使用mongo / mongoose通过_id以外的字段查找文档 - How to find a document by an field other than _id using mongo/mongoose 在 MongoDB 文档中,使用 Node JS,如何找到数组中的最后一项,并将文档插入到该项的子数组中? - In a MongoDB document, using Node JS, how do I find the last item in an array, and insert a document into said item's sub-array? 猫鼬通过子文档的_id查找文档 - mongoose find document by subdocument's _id ZCCADCDEDB567ABAE643E15DCF0974E503Z:通过数组字段中的值查找文档 - Mongoose: find document by values inside an array field 如何在 MongoDB 中的数组中获取嵌入文档(使用 Mongoose) - How to get embedded document in an array in MongoDB (with Mongoose) 在mongo文档中找到数组中的最后一项,并检查其字段是否为某个值 - Find the last item in array in mongo document and check if its field is a certain value 如何在猫鼬数组中查找和删除项目 - How to find by and delete an item in a mongoose array 在MongoDB中查找具有字符串ID数组的文档 - Find documents with array of string ID's in MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM