简体   繁体   English

如何在猫鼬中查找数组中的文档

[英]How to find documents in array in mongoose

How can i use mongoose.find() method to find the below code.我如何使用 mongoose.find() 方法来查找以下代码。

Paid.find({info:{data:"status"}}) This is not working as it is returning empty array Paid.find({info:{data:"status"}})这不起作用,因为它返回空数组

info{
  event: 'charge.success',
  data: {
    id: 1190118808,
    domain: 'test',
    status: 'success',
    reference: 'T458573232919212',
    amount: 100000,
    message: null,
    gateway_response: 'Successful',
    paid_at: '2021-06-26T00:25:33.000Z',
    created_at: '2021-06-26T00:25:24.000Z',
}

If you use mongoose, it should be as easy as doing YourModel.find({ data: { status: "success" } }) , this is assuming info is not an actual property on the document如果您使用猫鼬,它应该像执行YourModel.find({ data: { status: "success" } }) ,这是假设info不是文档上的实际属性

Full Example (including data structure (the Schema), simplified):完整示例(包括数据结构(架构),简化):

const schema1 = new mongoose.Schema({ 
  data: {
    status: String
  }
});
const model1 = mongoose.model("model1", schema1);

(async () => {
  // connect the connection before this part
  await model1.create({ data: { status: "success" } });
  await model1.create({ data: { status: "failure" } });

  // this should only find & log the "success" one in the array
  const found = await model1.find({ data: { status: "success" } }).exec();
  console.log("found", found);
})()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM