简体   繁体   English

Mongoose find() 不适用于国外 object

[英]Mongoose find() not working on foreign object

Here's my schema.这是我的架构。

const Sample = new Schema({
  name: { type: String },
  people: { type: Schema.Types.ObjectId, ref: 'People' }
})

const People = new Schema({
  name: { type: String }
})

Now when I am trying to query the name of People schema using Sample schema.现在,当我尝试使用示例架构查询人员架构的名称时。

var queryString = 'name of people'
const results = await Sample.find().populate('people').find({
  'people.name': { $regex: queryString, $options: 'i' },
})

It's working if I am just querying the name of Sample schema but when I'm trying to query the name of People schema on Sample model there are no results.如果我只是查询 Sample 模式的名称,但当我尝试查询 Sample model 上的 People 模式的名称时,它会起作用,但没有结果。

Mongoose populate supports applying query conditions: Mongoose populate 支持申请查询条件:

const results = await Sample.find()
    .populate({path: 'people', name: {'APPLY YOUR QUERY CONDITIONS HERE'}});

Check the official documentation:查看官方文档:

https://mongoosejs.com/docs/populate.html#query-conditions https://mongoosejs.com/docs/populate.html#query-conditions

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

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