英文:
MongoDB Aggregation: Return the object in an array which contains a matching value
问题
在我的MongoDB聚合管道中,我想从数据中检索与数字 "9999933333" 匹配的对象(见下文)。
对于这个数字 "9999933333",我希望结果如下:
'matchingObjects': [
{
id: 'efg',
phoneNumbers: ['9999933333','9999944444']
},
{
id: 'hij',
phoneNumbers: ['9999933333','9999955555']
}
]
以下是数据(经过前几个阶段之后的数据):
{
id: 123
contactsOfAppUsers:[
{
id:'abc',
contactsArray: ['9999911111','9999922222']
},
{
id:'efg',
contactsArray: ['9999933333','9999944444']
},
{
id:'hij',
contactsArray: ['9999955555','9999933333']
}
]
}
我尝试了以下操作,但它返回布尔值,不是我想要的结果。
db.phNumbers.aggregate([
{// Previous stage},
{
$addFields: {
'matchingObjects': {
'$map': {
'input': '$contactsOfAppUsers',
'as': 'cc',
'in': {
'$in': [
'9999933333', '$$cc.contactsArray'
]
}
}
}
}
},
])
英文:
In my MongoDB aggregation pipeline, I want to retrieve the matching objects for a number from the data (see below)
For this number "9999933333", I would like the result like this:
'matchingObjects':[
{
id:'efg',
phoneNumbers: ['9999933333','9999944444']
},
{
id:'hij',
phoneNumbers: ['9999933333','9999955555']
}
]
Here is the Data (after previous stages):
{
id: 123
contactsOfAppUsers:[
{
id:'abc',
contactsArray: ['9999911111','9999922222']
},
{
id:'efg',
contactsArray: ['9999933333','9999944444']
},
{
id:'hij',
contactsArray: ['9999955555','9999933333']
}
]
}
I tried this, which gives boolean values which is not what I want.
db.phNumbers.aggregate([
{// Previous stage},
{
$addFields: {
'matchingObjects': {
'$map': {
'input': '$contactsOfAppUsers',
'as': 'cc',
'in': {
'$in': [
'9999933333','$$cc.contactsArray'
]
}
}
}
}
},
])
答案1
得分: 1
你可以尝试这个:
db.collection.aggregate({
$project: {
_id: 0,
matchingObjects: {
$filter: {
input: "$contactsOfAppUsers",
as: "numbers",
cond: {
$in: [
"9999933333",
"$$numbers.contactsArray"
]
}
}
}
}
})
参考:https://mongoplayground.net/p/Q5xlO1ZOMcb
编辑
如果你真的想要将字段重命名为 phoneNumbers
,可以尝试这个:
db.collection.aggregate([
{
$project: {
_id: 0,
matchingObjects: {
$filter: {
input: "$contactsOfAppUsers",
as: "numbers",
cond: {
$in: [
"9999933333",
"$$numbers.contactsArray"
]
}
}
}
}
},
{
$addFields: {
matchingObjects: {
$map: {
input: "$matchingObjects",
as: "matchingObjects",
in: {
phoneNumbers: "$$matchingObjects.contactsArray",
id: "$$matchingObjects.id"
}
}
}
}
}
])
参考:https://mongoplayground.net/p/cXkFo59YdMy
英文:
You should try this:
db.collection.aggregate({
$project: {
_id: 0,
matchingObjects: {
$filter: {
input: "$contactsOfAppUsers",
as: "numbers",
cond: {
$in: [
"9999933333",
"$$numbers.contactsArray"
]
}
}
}
}
})
See: https://mongoplayground.net/p/Q5xlO1ZOMcb
EDIT
If you really want to rename the field to phoneNumbers
, try this:
db.collection.aggregate([
{
$project: {
_id: 0,
matchingObjects: {
$filter: {
input: "$contactsOfAppUsers",
as: "numbers",
cond: {
$in: [
"9999933333",
"$$numbers.contactsArray"
]
}
}
}
}
},
{
$addFields: {
matchingObjects: {
$map: {
input: "$matchingObjects",
as: "matchingObjects",
in: {
phoneNumbers: "$$matchingObjects.contactsArray",
id: "$$matchingObjects.id"
}
}
}
}
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论