嵌套数组聚集仅用于匹配一个字段
我在 mongodb 中有以下 json 数据。
[
{
"_id": 1,
"name": "user1",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 2,
"name": "user2",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 3,
"name": "user3",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 4,
"name": "user4",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 5,
"name": "user5",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 6,
"name": "user6",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 7,
"name": "user7",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 8,
"name": "user8",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
}
]
我正在查询分页数据,如下所示。
[
{
"$match": {
"userActivities": {
"actionTaken": "Sign in."
}
}
},
{
"$sort": {
"name": -1
}
},
{
"$project": {
"name": 1,
"userActivities": {
"$filter": {
"input": "$userActivities",
"as": "userActivities",
"cond": {
"$eq": [
"$$userActivities.actionTaken",
"Sign in."
]
}
}
}
}
},
{
"$skip": 2
},
{
"$limit": 2
}
]
但即使符合过滤条件,也无法获取任何数据。如果我从示例数据中删除 progress
字段,那么它会为我提供数据。
当要查询的列中有多个字段时,是否应该采取不同的做法?
I have following json data in mongodb.
[
{
"_id": 1,
"name": "user1",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 2,
"name": "user2",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 3,
"name": "user3",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 4,
"name": "user4",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 5,
"name": "user5",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 6,
"name": "user6",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 7,
"name": "user7",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
},
{
"_id": 8,
"name": "user8",
"userActivities": [
{
"actionTaken": "Sign in.",
"progress": "Success."
},
{
"actionTaken": "logout",
"progress": "Success."
}
]
}
]
I am querying data for pagination as following.
[
{
"$match": {
"userActivities": {
"actionTaken": "Sign in."
}
}
},
{
"$sort": {
"name": -1
}
},
{
"$project": {
"name": 1,
"userActivities": {
"$filter": {
"input": "$userActivities",
"as": "userActivities",
"cond": {
"$eq": [
"$userActivities.actionTaken",
"Sign in."
]
}
}
}
}
},
{
"$skip": 2
},
{
"$limit": 2
}
]
But not getting any data even though it matches filter criteria. If I remove progress
field from sample data then it gives me data.
Is it something should be done differently when multiple fields are there in column to query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的
$ Match
阶段中使用点符号。示例mongo playground
Use dot notation in your
$match
stage.Sample Mongo Playground
通过一个人的帮助得到答案。我不得不使用$ lemmatch如下。
Got answer by taking help from one person. I had to use $elemMatch as following.
也许在匹配阶段的其他方面添加“进度”您仅与单个field ActionTaken匹配,该对象在集合中不存在:
样品Mongo游乐场
Maybe add the "progress" in your match stage otherways you match only objects with the single field actionTaken that do not exist in the collection :
Sample Mongo Playground