在mongoose中查询对象数组内部的值可以使用以下方法:
db.users.find({ friends: { $elemMatch: { age: 25 } } });
db.users.find({ "friends.age": 25 });
这将返回包含age字段等于25的所有用户。
db.users.aggregate([
{ $unwind: "$friends" }, // 展开数组
{ $match: { "friends.age": 25 } }, // 进行匹配
{ $group: { _id: "$_id", friends: { $push: "$friends" } } } // 重新组合结果
]);
这将返回包含age字段等于25的所有用户,并将结果重新组合为包含匹配的friends数组的文档。
以上是在mongoose中查询对象数组内部的值的几种方法。根据具体的需求和数据结构,选择适合的方法进行查询。
没有搜到相关的结果
领取专属 10元无门槛券
手把手带您无忧上云