从 Mongodb 中的对象数组中查找对象
const Schema = new Schema({
queueId: {
type: String,
required: true,
index: {
unique: true
}
},
players: {
type: [
{
ID: {
type: String,
required: true,
index: {
unique: true
},
default: 'null'
},
name: {
type: String,
required: true,
default: 'null',
},
queueId: {
type: String,
required: true,
default: 'null'
}
}
],
required: true,
default: []
},
isAvailable: {
type: Boolean,
required: true,
default: true
},
isFull: {
type: Boolean,
required: true,
default: false
}
});
如何使用 findOne()
从 players
获取对象,这是一个数组
我目前正在尝试此代码,但它
const doc = await List.findOne({ players: { ID: 'id' } });
基本上返回 null
players
是一个数组,我想从对象的players数组中查找ID并获取文档。
const Schema = new Schema({
queueId: {
type: String,
required: true,
index: {
unique: true
}
},
players: {
type: [
{
ID: {
type: String,
required: true,
index: {
unique: true
},
default: 'null'
},
name: {
type: String,
required: true,
default: 'null',
},
queueId: {
type: String,
required: true,
default: 'null'
}
}
],
required: true,
default: []
},
isAvailable: {
type: Boolean,
required: true,
default: true
},
isFull: {
type: Boolean,
required: true,
default: false
}
});
How do I use findOne()
to get the object from players
which is an array
Im currently trying this code but it returns null
const doc = await List.findOne({ players: { ID: 'id' } });
basically players
is an array and I want to find ID from the players array of objects and get the document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mongoplayground
mongoplayground
mongoplayground
mongoplayground
mongoplayground
mongoplayground