我有以下文件:
{
array: [
{
type: 'error',
data: 1
},
{
type: 'error',
data: 2
}
]
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我是否只能获取每个数组元素中的数据字段并将其作为数组返回?就像下面这样:
[1, 2] // <--- array containing only the data fields
Run Code Online (Sandbox Code Playgroud)
mongodb 投影文档似乎没有涵盖这一点?
我有这样的文档集
{ "owner": "550511223", "file_name": "55234OT01", "file_type": "other", "comment": "Just fix it"},
{ "owner": "550510584", "file_name": "55584RS01", "file_type": "resume", "comment": "Good enough"},
{ "owner": "550511223", "file_name": "55234AP01", "file_type": "applicant", "comment": "This will do"}
Run Code Online (Sandbox Code Playgroud)
我需要一个像这样的对象的结果
{
[{
"owner" : "550510584",
"files" : [{"file_name": "55584RS01","file_type": "resume","comment": "Good enough"}],
},{
"owner" : "550511234",
"files" : [{"file_name": "55234AP01","file_type": "applicant","comment": "This will do"},
{"file_name": "55234OT01","file_type": "other","comment": "Just fix it"}]
}]
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来做到这一点。我尝试过分组和聚合,但我只能将 file_name 字段推入,因为我搞乱了语法
有没有办法用 spring data 的 mongodb 聚合返回游标?
Aggregation agg = newAggregation(
match(Criteria.where("_id").is(objId)),
unwind("taskResultContent"),
project("taskResultContent.executionUUID","taskResultContent.returnContent","taskResultContent.sequency").and("resultID").previousOperation(),
match(Criteria.where("executionUUID").is(executionUUID)),
sort(DESC,"sequency")
).withOptions(Aggregation.newOptions().cursor(cursor).build());
Run Code Online (Sandbox Code Playgroud) 我有以下聚合查询:
db.radars.aggregate([
{
$group: {
_id: '$locationId',
count: {$sum: 1}
},
},
{
$match: {
loggedAt: {
$gte: new Date('2014-01-01T00:00:00Z'),
$lte: new Date('2016-12-31T00:00:00Z')
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
我肯定有符合条件的数据:
但我什么也没得到:
有谁知道为什么会发生这种情况?
我正在尝试使用 Aggregates.project 对文档中的数组进行切片。我的文件就像
{
"date":"",
"stype_0":[1,2,3,4]
}
Run Code Online (Sandbox Code Playgroud)
我的java代码是:
Aggregates.project(Projections.fields(
Projections.slice("stype_0", pst-1, pen-pst),Projections.slice("stype_1", pst-1, pen-pst),
Projections.slice("stype_2", pst-1, pen-pst),Projections.slice("stype_3", pst-1, pen-pst))))
Run Code Online (Sandbox Code Playgroud)
最后我得到了错误
First argument to $slice must be an array, but is of type: int
Run Code Online (Sandbox Code Playgroud)
我想这是因为 stype_0 中的第一个元素是 int ,但我真的不知道为什么?多谢!
我在 mongodb 聚合中有一个匹配表达式。匹配中包含 3 个字段,但它们并不总是包含数据。我只想在字段不为空时将字段包含在匹配中。
如果所有字段都有数据,则匹配的样子,但例如,如果用于的数组为studentGradeLevels空,那么我不想包含它,或者我希望查询仍然返回数据而忽略空参数。
$match: {
"school._id": "7011",
"studentGradeLevels": { $in: ["09", "10", "11", "12"] },
"contentArea": {
$in: [
"English 1"
]
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法动态构建匹配,以便我只包含我想要的字段(如果它们为空或不为空),或者在查询中执行某些操作,以便忽略空参数。
我正在使用以下 mongo 数据库查询。它只向我显示我的角色数据的年龄组。
db.amplifyindex.aggregate([
{ $unwind: "$demographic" },
{ $match : { 'demographic.is_latest':"active",
'demographic.date_of_birth' : { $exists : true} } },
{ $project : {"ageInMillis" : {$subtract : [new Date(),
"$demographic.date_of_birth"] } } },
{ $project : {"age" : {$divide : ["$ageInMillis", 31558464000] }}},
{ $project : {"age" : {$subtract : ["$age", {$mod : ["$age",1]}]}}},
])
Run Code Online (Sandbox Code Playgroud)
该查询的结果对象是:
{
"_id" : ObjectId("58a42cbbdb5d880c1e000029"),
"age" : 29.0
}
Run Code Online (Sandbox Code Playgroud)
我想以如下方式获取数据:
{
"_id" : ObjectId("58a42cbbdb5d880c1e000029"),
"age" : 10-30
"personas" : 10
}
Run Code Online (Sandbox Code Playgroud)
我的场景中完整的数据结构如下:
{
"_id" …Run Code Online (Sandbox Code Playgroud) 我在 MongoDB 中有一个数据集,如下所示:
{ "name": "Tom's", "category": "coffee shop" },
{ "name": "Red Lobster", "category": "restaurant" },
{ "name": "Tom's", "category": "coffee shop" },
{ "name": "Starbucks", "category": "coffee shop" },
{ "name": "Central Park", "category": "park" },
{ "name": "Office", "category": "office" },
{ "name": "Red Lobster", "category": "restaurant" },
{ "name": "Home", "category": "home" },
{ ... } // and so on
Run Code Online (Sandbox Code Playgroud)
如何找到特定类别最常见的值?例如,最常见出现的值coffee shop和restaurant应该分别是 Tom's 和 Red Lobster。
我当前的$aggregate查询似乎只列出了所有数据集中最常见的值:
db.collection.aggregate(
{ …Run Code Online (Sandbox Code Playgroud) 示例文档:
{
"_id" : ObjectId("5e7331614dd99d1a30143a58"),
"status" : "rejected",
"from" : ISODate("2020-03-17T08:46:02.552Z"),
"to" : ISODate("2020-03-18T08:46:07.124Z")
},
{
"_id" : ObjectId("5e7331614dd99d1a30143a58"),
"status" : "rejected",
"from" : ISODate("2020-03-02T08:08:32.819Z"),
"to" : ISODate("2020-03-05T08:08:37.125Z"),
}
Run Code Online (Sandbox Code Playgroud)
我是 mongodb 的新手,我想计算每个文档的日期(到字段和从字段)之间的天数,其中状态等于“已拒绝”
Mongo 的新手,发现了很多使用聚合框架从字符串数组中删除重复项的示例,但我想知道是否可以根据对象中的字段从对象数组中删除重复项。例如
{
"_id" : ObjectId("5e82661d164941779c2380ca"),
"name" : "something",
"values" : [
{
"id" : 1,
"val" : "x"
},
{
"id" : 1,
"val" : "x"
},
{
"id" : 2,
"val" : "y"
},
{
"id" : 1,
"val" : "xxxxxx"
}
]
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想根据该id领域删除欺骗。所以最终会
{
"_id" : ObjectId("5e82661d164941779c2380ca"),
"name" : "something",
"values" : [
{
"id" : 1,
"val" : "x"
},
{
"id" : 2,
"val" : "y"
}
]
}
Run Code Online (Sandbox Code Playgroud)
选择具有给定 id 的第一个/任何对象。只想最终每个 …