我正在进行mongodb集合的聚合。我的mongodb集合在时间戳中具有creation_time。如何收集当天的结果并与第二天的结果进行聚合。假设我必须收集5天的数据。我的mongodb集合是:
{
"_id" : "1522845653126"
},
{
"_id" : "1522838153324"
},
{
"_id" : "1513421466415"
},
{
"_id" : "1515488183153"
},
{
"_id" : "1521571234500"
}
Run Code Online (Sandbox Code Playgroud)
我该如何计算?在特定日期保存了多少项?假设我要查询的结果是返回汇总的运行总计,例如:
{
time: "2013-10-10"
count: 3,
},
{
time: "2013-10-11"
total: 7,
}
Run Code Online (Sandbox Code Playgroud)
这样的事情。我试图做这样的事情
db.coll.aggregate([
{
$group:{
_id:new Date($creation_time).toLocaleDateString(),
$count:{$add:1}
}
time:new Date($creation_time).toLocaleDateString()
}
])
Run Code Online (Sandbox Code Playgroud)
它不起作用。我在哪里做错了?我是mongodb的新手。谢谢你的帮助
我同时也是aws和mongodb的新手,所以我在一个非常基本的方面试图连接到我的mongo数据库,托管在亚马逊linux ec2实例上.原因是,我无法构建数据库的路径.
这是我正在尝试使用的:mongoose.connect('mongod://ec2-user@ec2-XX-XX-XXX-XXX-XX.compute-1.amazonaws.com:27017/test' ).
这是我的测试lambda函数的结果: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: URL malformed, cannot be parsed
我正在使用mongodb 3.6.5.
我想获得完整的数字 a String,但"1490650000000"它返回科学记数法"1.49065e+12"
这是我尝试转换它的方法:
{$substr: ["$myNumber", 0, -1]};
Run Code Online (Sandbox Code Playgroud)
有什么想法如何预防吗?
注意:
我使用的是v3.6,无法升级使用$toString(感谢mlab)。
scientific-notation tostring mongodb mongodb-query aggregation-framework
我使用聚合框架对多个字段进行分组
{
_id:{_id:"$_id",feature_type:"$feature_type",feature_name:"$feature_name"},
features: { $push: "$features" }
}
Run Code Online (Sandbox Code Playgroud)
它给出的结果是
{_id:
{_id:1,feature_type:"Test",feature_name:"Tests"},
features:["23423","32423","2342342"]
}
Run Code Online (Sandbox Code Playgroud)
但我想要这样的结果
{_id:1,feature_type:"Test",feature_name:"Tests",
features:["23423","32423","2342342"]
}
Run Code Online (Sandbox Code Playgroud)
我如何使用聚合框架来实现这一点。
我正在尝试执行一个棘手的聚合以返回集合中文档内嵌套数组的大小。
以下是重新创建示例数据的方法:
db.test.insert({
projects: [
{
_id: 1,
comments: [
'a',
'b',
'c'
]
},
{
_id: 2,
comments: [
'a',
'b'
]
},
{
_id: 3,
comments: []
}
]
})
Run Code Online (Sandbox Code Playgroud)
我要执行的聚合在这里:
db.test.aggregate([
// enter aggregation here
])
Run Code Online (Sandbox Code Playgroud)
这是所需的输出:
[{
projects: [
{
_id: 1,
comment_count: 3
},
{
_id: 2,
comment_count: 2
},
{
_id: 3,
comment_count: 0
}
]
}]
Run Code Online (Sandbox Code Playgroud)
我正在为如何写这个而苦苦挣扎。如果我尝试以下操作:
"projects.comment_count": {"$size": }
结果返回结果数组的大小:
[{
projects: [
{
_id: 1,
comment_count: 3
},
{
_id: 2, …Run Code Online (Sandbox Code Playgroud) 我有一个查询,使用$ lookup “联接”两个模型,此后,我使用$ project选择所有需要的字段,但是$ project带来了很多对象(user_detail),其中包含我需要的更多数据。我只想要结果的两个字段(scheduleStart和scheduleEnd)。
我的查询:
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday",
lastName: "$lastname"
},
totalServices: {
$sum: "$services"
},
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{
$project: {
_id: 1,
name: 1,
name: 1,
cpf: 1,
phone: 1,
email: 1,
birthday: 1,
totalServices: …Run Code Online (Sandbox Code Playgroud) 这是我的收藏
{
"_id" : ObjectId("5c225f9a66d39d55c036fa66"),
"name" : "Sherlock",
"mobile" : "999999",
"adress" : [
{
"street" : "221b baker street",
"city" : "london"
},
{
"street" : "ben street",
"city" : "london"
}
],
"tags" : [
"Detective",
"Magician",
"Avenger"
]
}Run Code Online (Sandbox Code Playgroud)
现在我想获取地址数组中的第一个或第二个值。为此我正在使用这个命令。
> db.agents.findOne({"name" : "Sherlock"},{"adress" : 1})Run Code Online (Sandbox Code Playgroud)
但它不是给出单个结果,而是给出整个数组,例如
{
"_id" : ObjectId("5c225f9a66d39d55c036fa66"),
"adress" : [
{
"street" : "221b baker street",
"city" : "london"
},
{
"street" : "ben street",
"city" : "london"
}
]
}Run Code Online (Sandbox Code Playgroud)
可以通过比较数组值来完成,例如
db.agents.find({"adress.street": …Run Code Online (Sandbox Code Playgroud)我试图获取一个集合中的所有文档,其中的日期存储为:
{
"_id": {
"$oid": "5c500ff8df157e051961cfab"
},
"date": {
"$date": {
"$numberLong": "1548750839931"
}
},
"when": "2019-1-29T8:33Z",
"score": 20
}
Run Code Online (Sandbox Code Playgroud)
...大于此:1546300800000
我的收藏中有 2 条记录,但我没有得到任何回报?
这是我的查询:
{"date":{"$gt": 1546300800000}}
Run Code Online (Sandbox Code Playgroud)
我想这与将日期存储为字符串有关,但可以做到吗?
希望得到帮助,并在此先感谢:-)
我想为我的收藏中的每个用户获取具有最高价值的记录的评论。
//myCol
[
{'user' : 1, 'value':20, "comment": "cloudy", "date":"2018-12-01"},
{'user' : 2, 'value':5, "comment": "sun","date":"2018-12-01"},
{'user' : 3, 'value':13, "comment": "rain","date":"2018-13-01"},
{'user' : 4, 'value':13, "comment": "cloudy","date":"2018-13-01"},
{'user' : 2, 'value':20, "comment": "rain","date":"2018-01-20"},
{'user' : 1, 'value':2, "comment": "cloudy","date":"2018-02-02"},
]
Run Code Online (Sandbox Code Playgroud)
会给出以下结果:
//Result
[
{'user' : 1, 'value':20, "comment": "cloudy", "date":"2018-12-01"},
{'user' : 2, 'value':20, "comment": "rain","date":"2018-01-20"},
{'user' : 3, 'value':13, "comment": "rain","date":"2018-13-01"},
{'user' : 4, 'value':13, "comment": "cloudy","date":"2018-13-01"},
]
Run Code Online (Sandbox Code Playgroud)
对于 MySQL,我会使用一些连接逻辑
SELECT user FROM myTable as t0
LEFT JOIN …Run Code Online (Sandbox Code Playgroud) 有数百个线程和运算符可以将日期对象转换为 ISO 字符串,但作为 Node.js 中 mongoose/mongodb 聚合运算符的一部分,找不到任何相反的方法。
我有一个目前正在生产中使用的旧版 mongodb 3.6,我有一个管道,我试图将一个普通的 ISO 日期对象从 mongo 转换为一个 UNIX 时间戳,以便我可以将它与 ngx-charts 和其他图表一起使用。
我不能使用 $toDate、$convert 运算符或 $dateFromString 因为我需要的选项在 3.6 中不可用
到目前为止,我已经尝试过这种变化:
$project: {
_id: 0,
// name: '$_id._id',
value: '$_id.count',
name: new Date.parse('$_id.date').getTime(),
min: '$min',
max: '$max'
}
Run Code Online (Sandbox Code Playgroud)
但是这些都不起作用,因为聚合是在 db 上处理的,并且不知道该函数是什么。我查看了许多运算符,并尝试先转换为字符串,然后再转换回 UNIX 日期,但似乎没有任何可用于从该 ISO 日期转换为 javascript/unix 时间戳的内容。