我有一个包含数组的文档。就像这样:
"_id" : ObjectId("55101f81e4b07caf8554b9b1"),
"myId" : "1222222",
"isDelayed" : false,
"status" : "BALLS",
"yellow" : false,
"white" : true,
"people" : [
{
"peopleId" : 222222,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
{
"peopleId" : 33312,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
...
Run Code Online (Sandbox Code Playgroud)
我有一个如下所示的 mongo 查询,在 collection 中mycoll,如果myId=1.222在 people 数组中,如果people.peopleId=1123它返回第一个匹配项:
db.getCollection('mycoll').find(
{myId:'1.222',
people: { $elemMatch: { peopleId: 1123 …Run Code Online (Sandbox Code Playgroud) 我正在尝试按用户和电子邮件进行分组,并且仅输出小计> 1。我尝试了此操作,但编译失败。
db.member.aggregate(
{"$group" : {
_id : {user:"$user", email: "$email"},
count : { $sum : { if: { $gte: [ "$sum", 1 ] }, then: 1, else: 0 }
} } } )
Run Code Online (Sandbox Code Playgroud) 我有一个名为 的 MongoDB 集合bios,其中包含类似于以下内容的文档:
{
"_id" : ObjectId("51df07b094c6acd67e492f41"),
"name" : {
"first" : "John",
"last" : "McCarthy"
},
"birth" : ISODate("1927-09-04T04:00:00Z"),
"death" : ISODate("2011-12-24T05:00:00Z"),
"contribs" : [
"Lisp",
"Artificial Intelligence",
"ALGOL"
]
},
{
"_id" : 3,
"name" : {
"first" : "Grace",
"last" : "Hopper"
},
"title" : "Rear Admiral",
"birth" : ISODate("1906-12-09T05:00:00Z"),
"death" : ISODate("1992-01-01T05:00:00Z"),
"contribs" : [
"UNIVAC",
"compiler",
"FLOW-MATIC",
"COBOL"
]
}
Run Code Online (Sandbox Code Playgroud)
我的目标是检索集合中每个文档的数组 contribs 的第二个元素bios。使用新的聚合管道运算符$filter我运行以下查询:
> db.bios.aggregate([
{
$match: {"contribs.2":{"$exists":1}}}, …Run Code Online (Sandbox Code Playgroud) 在我的 MongoDB 后端中,我想创建一个端点,该端点返回名为department. 现在,如果我在 IntelliShell 中执行此操作,我会执行以下操作:
db.staffmembers.distinct( "department" )
Run Code Online (Sandbox Code Playgroud)
这将返回一个包含所有值的数组department。
但是如何在像这样的 Mongoose find() 查询中返回所有唯一值?
Staffmember.find({ name: 'john', age: { $gte: 18 }});
Run Code Online (Sandbox Code Playgroud)
换句话说,如果我想使用find()上面的like 来返回department“staffmembers”集合中的所有唯一值,语法会是什么样的?
我有一个从 3 个集合中获取结果的聚合查询。
我正在使用 mongoDb 3.4
来自结果的一份样本文档。
{
"_id" : ObjectId("5ba1717ee4b00ce08ca47cfa"),
"name" : "captain jack",
"email" : "jack@gmail.com",
"mobile" : "9000000023",
"status" : "verified",
"courses" : [
{
"_id" : "13",
"name" : "Course (03)"
},{
"_id" : "12",
"name" : "Course (03)"
}
],
"examCompleted" : false,
"login" : "5ba1717ee4b00fe08ca47cfa",
"partnerMetaInfo" : {
"_id" : ObjectId("5ba1717ee4b00fe08ca47cfa"),
"costCode" : "5761",
"hub" : "CALCUTTA",
"location" : "Kolkata"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试引入partnerMetaInfo根级别。我也无法在 _id == 13 上使用 $match 过滤courses._id
这是我的汇总查询:
db.getCollection("mainCollection").aggregate([
{ …Run Code Online (Sandbox Code Playgroud) 位置点另存为
{
"location_point" : {
"coordinates" : [
-95.712891,
37.09024
],
"type" : "Point"
},
"location_point" : {
"coordinates" : [
-95.712893,
37.09024
],
"type" : "Point"
},
"location_point" : {
"coordinates" : [
-85.712883,
37.09024
],
"type" : "Point"
},
.......
.......
}
Run Code Online (Sandbox Code Playgroud)
有几个文件。我需要到group最近的地点。分组后第一个第二个位置将在一个文档中,第三个在第二个文档中。请注意,第一和第二的位置点不相等。两个都是最近的地方。
有什么办法吗?提前致谢。
我正在尝试按集合中的多个属性对文档进行类型安全的分组。我发现问题不在于按多个属性本身进行分组,而是键是对象 ( new { }) 而不是字符串。
这有效:
collection.Aggregate().Group(x => x.Name, x => new { Name = x.Key, Count = x.Sum(s => 1) }).ToList();
Run Code Online (Sandbox Code Playgroud)
这不起作用:
collection.Aggregate().Group(x => new { Name = x.Name }, x => new { Name = x.Key.Name, Count = x.Sum(s => 1) }).ToList();
Run Code Online (Sandbox Code Playgroud)
错误是:
MongoDB.Driver.MongoCommandException:“命令聚合失败:字段“名称”必须是累加器对象。
当我将不起作用的查询转换为字符串时,我得到以下信息:
aggregate([{ "$group" : { "_id" : { "Name" : "$Name" }, "Name" : "$_id.Name", "Count" : { "$sum" : 1 } } }])
Run Code Online (Sandbox Code Playgroud)
我想这里的问题是这部分"Name" : "$_id.Name"。我该如何解决这个问题?
我目前使用的是 2.8.0 版的 …
c# mongodb aggregation-framework .net-core mongodb-.net-driver
我有两个日期Start Date: 2019-08-13和End Date: 2019-08-20。我想结束日期的差异在工作日(节假日除外)项的开始日期。所以我的成绩会4作为2019-08-15一个节日和2019-08-17,2019-08-18是周末。
aggregate-functions mongodb mongodb-query aggregation-framework
我有一个聚合管道并且被困在一个舞台上。此时我有一个格式如下的文档:
{
_id:ObjectId(5e3d326637df7e4dda73ec22),
levelName:"Level 1",
levelNames: {
"Level 1":"5e567993b6ed4b7b4d2c044d"
"Level 2":"5e567996a7826d45f836dfa3"
"Level 3":"5e5679991f515a01c73e9006"
}
}
Run Code Online (Sandbox Code Playgroud)
并且我想添加一个 $addField/$set 阶段,它为我提供 levelNames 对象中条目的 ID 值,其键与根文档的 levelName 属性匹配。
我一直在尝试使用某种动态字段路径 ($concat: ["levelNames.", "$levelName"]) 但没有成功。这是我的 $addField 阶段:
/**
* newField: The new field name.
* expression: The new field expression.
*/
{
currentLevelId: {
$let: {
vars: {
levelPath: {
$concat: ["levelNames.", "$levelName"]
}
},
in: {
currentLevelId: "$$levelPath"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
{
_id:ObjectId(5e3d326637df7e4dda73ec22),
levelName:"Level 1",
levelNames: {
"Level 1":"5e567993b6ed4b7b4d2c044d"
"Level 2":"5e567996a7826d45f836dfa3"
"Level 3":"5e5679991f515a01c73e9006" …Run Code Online (Sandbox Code Playgroud) 我需要从数组中提取第一项并将其添加到它自己的对象中,所以我发现 $first 正是这样做的https://docs.mongodb.com/manual/reference/operator/aggregation/first-array-element /#example
但是,我收到错误Unrecognized expression '$first'并认为这是我的查询不起作用,所以我克隆了他们在他们的文档中的确切示例,但它给了我同样的错误。
db.runninglog.insertMany([
{ "_id" : 1, "team" : "Anteater", log: [ { run: 1, distance: 8 }, { run2: 2, distance: 7.5 }, { run: 3, distance: 9.2 } ] },
{ "_id" : 2, "team" : "Bears", log: [ { run: 1, distance: 18 }, { run2: 2, distance: 17 }, { run: 3, distance: 16 } ] },
{ "_id" : 3, "team" : "Cobras", log: [ { run: …Run Code Online (Sandbox Code Playgroud)