[英]How to check if an array of objects where I push into objects has a specific key value from another array of objects
[英]In Mongoose, how do you find an array of objects where an object has a specific key/value?
在Mongoose模型Person
,有一個稱為matches
的對象數組。
matches
看起來像
[
{
"$oid": "558ced061d35bd072e7b5825"
},
{
"$oid": "558ced061d35bd072e7b58a0"
},
{
"$oid": "558ced061d35bd072e7b58c6"
}
],
我想創建一個查詢,以查找在其matches
數組中具有ID的Person
。
問題在於,匹配查詢似乎僅適用於字符串數組,例如
models.Person.find({matches: 558ced061d35bd072e7b58c6})
但不適用於包含字符串值的對象數組。
有誰知道如何做到這一點?
您可以將字符串轉換為ObjectId並在查詢中使用它,如下所示:
var mongoose = require("mongoose"),
person_id = mongoose.Types.ObjectId("558ced061d35bd072e7b58c6");
models.Person.find({ "matches": person_id }).exec(callback);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.