Select 具有 MongoDB 条件的两个字段的不同组合(Node JS)
Select distinct combination of two fields with a condition in MongoDB (Node JS)
我有这种collection,名字叫'Message'
[
{
_id: new ObjectId("62641aea1fbe19349f8fba78"),
text: 'Hello',
user: new ObjectId("625b8f00e2464fb758263b4d"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7"),
createdAt: 2022-04-23T15:27:38.270Z,
updatedAt: 2022-04-23T15:27:38.270Z,
__v: 0
},
{
_id: new ObjectId("6264256fc0ee5093f8d994a0"),
text: 'Hi',
user: new ObjectId("62638f1495b841266161b032"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7"),
createdAt: 2022-04-23T16:12:31.155Z,
updatedAt: 2022-04-23T16:12:31.155Z,
__v: 0
},
{
_id: new ObjectId("62642dc05318104caabbdecc"),
text: 'Hi',
user: new ObjectId("625b8f00e2464fb758263b4d"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7"),
createdAt: 2022-04-23T16:48:00.416Z,
updatedAt: 2022-04-23T16:48:00.416Z,
__v: 0
}
]
这里用户是发送者,另一个接收者。如果我的用户 ID 是 "myId",
我想检索消息 objects 的 user 和 receiver 的不同组合,这意味着 user 的条件== myID 或接收者 == myId.
预期值。
[
{
user: new ObjectId("625b8f00e2464fb758263b4d"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7")
},
{
user: new ObjectId("62638f1495b841266161b032"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7")
}
]
所以我是 MongoDB 的新手,有什么可行的方法吗? 或者有什么我遗漏的吗?
欢迎 Sandeep Vithanage,
你可以这样做:
db.collection.aggregate([
{
$match: {
$or: [
{
user: myId
},
{
receiver: myId
}
]
}
},
{
$group: {
"_id": {
user: "$user",
receiver: "$receiver"
}
}
},
{
$replaceRoot: {
"newRoot": "$_id"
}
}
])
正如您在 playground 上看到的那样。
这是一个聚合管道。第一步是匹配您要求的案例。第二种是按用户和接收者的组合对它们进行分组。第三个只是格式化。
像下面这样使用 $match 和 $project:
await Model.aggregate([{
$match: {
$or: [
{user: 'myid'},
{receiver: 'myid'}
] }
},
$project: {user: 1, receiver: 1}
])
我有这种collection,名字叫'Message'
[
{
_id: new ObjectId("62641aea1fbe19349f8fba78"),
text: 'Hello',
user: new ObjectId("625b8f00e2464fb758263b4d"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7"),
createdAt: 2022-04-23T15:27:38.270Z,
updatedAt: 2022-04-23T15:27:38.270Z,
__v: 0
},
{
_id: new ObjectId("6264256fc0ee5093f8d994a0"),
text: 'Hi',
user: new ObjectId("62638f1495b841266161b032"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7"),
createdAt: 2022-04-23T16:12:31.155Z,
updatedAt: 2022-04-23T16:12:31.155Z,
__v: 0
},
{
_id: new ObjectId("62642dc05318104caabbdecc"),
text: 'Hi',
user: new ObjectId("625b8f00e2464fb758263b4d"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7"),
createdAt: 2022-04-23T16:48:00.416Z,
updatedAt: 2022-04-23T16:48:00.416Z,
__v: 0
}
]
这里用户是发送者,另一个接收者。如果我的用户 ID 是 "myId", 我想检索消息 objects 的 user 和 receiver 的不同组合,这意味着 user 的条件== myID 或接收者 == myId.
预期值。
[
{
user: new ObjectId("625b8f00e2464fb758263b4d"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7")
},
{
user: new ObjectId("62638f1495b841266161b032"),
receiver: new ObjectId("62638d3e5bcd98e7e48ca1b7")
}
]
所以我是 MongoDB 的新手,有什么可行的方法吗? 或者有什么我遗漏的吗?
欢迎 Sandeep Vithanage, 你可以这样做:
db.collection.aggregate([
{
$match: {
$or: [
{
user: myId
},
{
receiver: myId
}
]
}
},
{
$group: {
"_id": {
user: "$user",
receiver: "$receiver"
}
}
},
{
$replaceRoot: {
"newRoot": "$_id"
}
}
])
正如您在 playground 上看到的那样。 这是一个聚合管道。第一步是匹配您要求的案例。第二种是按用户和接收者的组合对它们进行分组。第三个只是格式化。
像下面这样使用 $match 和 $project:
await Model.aggregate([{
$match: {
$or: [
{user: 'myid'},
{receiver: 'myid'}
] }
},
$project: {user: 1, receiver: 1}
])