猫鼬:通过搜索查询获取对象
Mongoose: Get objects by a search query
我正在尝试使用猫鼬从我的 BSON 文档中提取两种类型的数据。
2个数据是;
- 意图 1 - 我想从项目数组中获取对象,其中 tagNo == 2
- 意图 2 - 我想从项目数组中获取对象,其中 tagNo == 2 和 tagID == kLawOURVpz1IIjoQ2fhCvy7NM。
对于意图1,我试过了;
db.find(
{ "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
{
"items": {
"$elemMatch": { "tagNo": 2 }
}
}
);
我厌倦了下面的意图 2
查询
db.find(
{ "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
{
"items": {
"$elemMatch": { "tagNo": 2, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", }
}
}
);
我一直在取回整个对象。
我的BSON文档是;
{
"_id": "ID_GOES_HERE",
"fileID": "0pdn3jdndndj3msms",
"fileName": "Item List",
"items": [
{
"tagNo": 2,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 5
},
{
"tagNo": 2,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 8
},
{
"tagNo": 2,
"status": 0,
"tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
"tagUnit": 81
},
{
"tagNo": 4,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 904
},
{
"tagNo": 3,
"status": 0,
"tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
"tagUnit": 24
},
{
"tagNo": 2,
"status": 0,
"tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
"tagUnit": 35
},
{
"tagNo": 1,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 11
},
{
"tagNo": 2,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 30
}
]
}
您需要聚合框架
db.collection.aggregate([
{
$match: { //Match condition to get the matching arrays
"fileID": "0pdn3jdndndj3msms",
"items.tagNo": 2
}
},
{
$project: {
"items": {
$filter: { //Project only matching array elements
input: "$items",
as: "item",
cond: {
$eq: [
"$$item.tagNo",
2
]
}
}
}
}
}
])
db.collection.aggregate([
{
$match: {
"items": {
"$elemMatch": {
"tagNo": 2,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
}
}
}
},
{
$project: {
"items": {
$filter: {
input: "$items",
as: "item",
cond: {
"$and": [
{
$eq: [
"$$item.tagNo",
2
]
},
{
$eq: [
"$$item.tagID",
"kLawOURVpz1IIjoQ2fhCvy7NM"
]
}
]
}
}
}
}
}
])
我正在尝试使用猫鼬从我的 BSON 文档中提取两种类型的数据。 2个数据是;
- 意图 1 - 我想从项目数组中获取对象,其中 tagNo == 2
- 意图 2 - 我想从项目数组中获取对象,其中 tagNo == 2 和 tagID == kLawOURVpz1IIjoQ2fhCvy7NM。
对于意图1,我试过了;
db.find(
{ "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
{
"items": {
"$elemMatch": { "tagNo": 2 }
}
}
);
我厌倦了下面的意图 2
查询db.find(
{ "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
{
"items": {
"$elemMatch": { "tagNo": 2, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", }
}
}
);
我一直在取回整个对象。
我的BSON文档是;
{
"_id": "ID_GOES_HERE",
"fileID": "0pdn3jdndndj3msms",
"fileName": "Item List",
"items": [
{
"tagNo": 2,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 5
},
{
"tagNo": 2,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 8
},
{
"tagNo": 2,
"status": 0,
"tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
"tagUnit": 81
},
{
"tagNo": 4,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 904
},
{
"tagNo": 3,
"status": 0,
"tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
"tagUnit": 24
},
{
"tagNo": 2,
"status": 0,
"tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
"tagUnit": 35
},
{
"tagNo": 1,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 11
},
{
"tagNo": 2,
"status": 0,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
"tagUnit": 30
}
]
}
您需要聚合框架
db.collection.aggregate([
{
$match: { //Match condition to get the matching arrays
"fileID": "0pdn3jdndndj3msms",
"items.tagNo": 2
}
},
{
$project: {
"items": {
$filter: { //Project only matching array elements
input: "$items",
as: "item",
cond: {
$eq: [
"$$item.tagNo",
2
]
}
}
}
}
}
])
db.collection.aggregate([
{
$match: {
"items": {
"$elemMatch": {
"tagNo": 2,
"tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
}
}
}
},
{
$project: {
"items": {
$filter: {
input: "$items",
as: "item",
cond: {
"$and": [
{
$eq: [
"$$item.tagNo",
2
]
},
{
$eq: [
"$$item.tagID",
"kLawOURVpz1IIjoQ2fhCvy7NM"
]
}
]
}
}
}
}
}
])