gpt4 book ai didi

node.js - 查询 Mongoose 子数组中的对象

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:05 25 4
gpt4 key购买 nike

我有一个如下所示的架构:

var LibrarySchema = new Schema({
id: String,
contactNumber: String,
collections: [{
id: String,
description: String
subCollections: [{
id: String,
description: String,
recentlyUpdated: Boolean
}, {
id: String,
description: String,
recentlyUpdated: Boolean
}]
}]
})

module.exports = mongoose.model('Library', LibrarySchema);

所有 ID 都是唯一的。一个区(另一个数组)内可以有多个图书馆。

我的问题是,如何查询嵌套数组以获得所需的对象?更准确地说,在给定库 ID、集合 ID 和子集合 ID 的情况下,如何获取特定的子集合对象。

最佳答案

您可以使用此查询:

Library.find({
'id': libraryID,
'collections.id': CollectionID,
'collections.subCollections.id': subCollectionID
}, { 'collections.subCollections.$': 1 }, function(err, data) {
console.log(err, data);
})

关于node.js - 查询 Mongoose 子数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37666138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com