我的收藏中有以下文件。每个文档都包含推文的文本和从推文中挑选出的一组实体(使用 AWS Comprehend):
{
"text" : "some tweet by John Smith in New York about Stack Overflow",
"entities" : [
{
"Type" : "ORGANIZATION",
"Text" : "stack overflow"
},
{
"Type" : "LOCATION",
"Text" : "new york"
},
{
"Type" : "PERSON",
"Text" : "john smith"
}
]
},
{
"text" : "another tweet by John Smith but this one from California and about Google",
"entities" : [
{
"Type" : "ORGANIZATION",
"Text" : "google"
},
{
"Type" : "LOCATION", …Run Code Online (Sandbox Code Playgroud) 我在 mongo 集合中有这个数据时间:
{
...
"CreationDateTime" : ISODate("2017-03-06T08:16:02.930Z"),
...
},
....
{
...
"CreationDateTime" : ISODate("2018-03-06T08:16:04.030Z"),
...
},...
Run Code Online (Sandbox Code Playgroud)
我想按小时对我的收藏进行分组。意思是我想让那些文件在 8 点钟进入同一组,那些文件在 9 点钟进入同一组。因为我想选择同一组中的第一个文档。有人对它的累加器运算符进行分组有任何逻辑或想法吗?
db.groups.aggregate([
{
$lookup:
{
from: "posts",
localField: "_id",
foreignField: "group",
as: "post"
}
}
])
Run Code Online (Sandbox Code Playgroud)
我收到群组和所有帖子的回复,例如.. [{geoup1,[post's array]}, {group2,[post's array]}]
如果有任何帖子,我只想将最后添加的帖子添加到帖子集合中
I have following collection
{
"_id" : ObjectId("5b16405a8832711234bcfae7"),
"createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
"firstName": "Bruce",
"lastName": "Wayne"
},
{
"_id" : ObjectId("5b16405a8832711234bcfae8"),
"createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
"firstName": "Clerk",
"lastName": "Kent"
},
{
"_id" : ObjectId("5b16405a8832711234bcfae9"),
"createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
"firstName": "Peter",
"lastName": "Parker"
}
Run Code Online (Sandbox Code Playgroud)
I need to $project one more key index with $concat with 'INV-00' + index of the root element
My output should be something like that
{
"_id" : ObjectId("5b16405a8832711234bcfae7"),
"createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
"firstName": "Bruce",
"lastName": "Wayne",
"index": "INV-001"
},
{ …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
我是 mongodb 聚合的新手。我的 mongo 文档有很多数组。我需要将其导出为平面文件。为此,我需要构建它。我尝试了以下聚合:
[
{$unwind : "$items" },
{$unwind : "$items.discounts"},
{$unwind : "$payments"},
{$unwind : "$payments.items"},
{$unwind : "$payments.refunds"}
]
Run Code Online (Sandbox Code Playgroud)
它起作用了,然后我只是添加了以下条件来过滤数据
[
{$match : { "updatedtime": { $gt: 1514764800000}}},
{$unwind : "$items" },
{$unwind : "$items.discounts"},
{$unwind : "$payments"},
{$unwind : "$payments.items"},
{$unwind : "$payments.refunds"}
]
Run Code Online (Sandbox Code Playgroud)
返回 0 行,我再次尝试使用 match 语句
[
{$match : { "updatedtime": { $gt: 1514764800000}}}
]
Run Code Online (Sandbox Code Playgroud)
它再次起作用。
[
{$match : { "updatedtime": { $gt: 1514764800000}}},
{$unwind : "$items" }
]
Run Code Online (Sandbox Code Playgroud)
作品。
[
{$match …Run Code Online (Sandbox Code Playgroud) 我有像下面这样的 MongoDB 文档:是否有查询以 subscribedGroups 的长度对文档进行排序?可以是升序或降序。
{
"_id" : ObjectId("58c29d9ec79d585c0e16b110"),
"subscribedGroups" : [
ObjectId("5b28a9190c8c0d0014b03a0c"),
ObjectId("5b2930650b813a0014a3294d"),
ObjectId("5b29f1b5243d470014d6d351")
]
}
Run Code Online (Sandbox Code Playgroud) 我已将数据与收藏夹分组,并已推入数组。我想使用对象键对数组进行排序,例如姓名、电子邮件或年龄。
{
$group:
{
_id: "$favourite",
favourite: { $first: "$favourite" },
data: {
$push: {
"name": "$name",
"email": "$email",
"age": "$age"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用下面的代码,但它不起作用。
{ "$sort": { "data.name": 1 } }
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
mongoose mongodb node.js mongodb-query aggregation-framework
我有一个 Mongo 集合,如下所示:
[{
id: 1,
timestamp: 1534488870841,
type: 'deposit'
}, {
id: 1,
timestamp: 1534488915119,
type: 'deposit'
}]
Run Code Online (Sandbox Code Playgroud)
如何查询以列出deposit按月份分组的所有交易。
必须使用timestamp属性(UNIX 毫秒)计算月份。
我可以按如下方式获得存款,但我不确定如何分组:
db.getCollection('transactions').find(
{"type":"deposit"});
Run Code Online (Sandbox Code Playgroud)
编辑:Mongo 3.4 版
我有一个这样的查询,我想按日期对我的结果进行排序。我在 DateTime 上有一个降序索引,在 UserId 上有一个升序索引,但是当我尝试按 DateTime 对我的结果进行排序时,它变得太慢了。
db.Users.aggregate([
{ "$match" : { "UserId" : { "$in" : [NUUID("1b029f8b-a17e-3172-9247-
9cddfaf9702b")] } } },
{ "$match" : { "DateTime" : { "$gte" : ISODate("2018-08-15T12:54:38Z"),
"$lte" : ISODate("2018-08-25T12:54:38Z") } } },
{ "$sort" : { "DateTime" : -1} }, { "$skip" : 0 }, { "$limit" : 20 }])
Run Code Online (Sandbox Code Playgroud)
当我删除排序部分时,它变得太快了。我尝试如下,它也太快了。
db.Users.aggregate([
{ "$match" : { "DateTime" : { "$gte" : ISODate("2018-08-15T12:54:38Z"),
"$lte" : ISODate("2018-08-25T12:54:38Z") } } },
{ "$match" : { "UserId" : …Run Code Online (Sandbox Code Playgroud)