我正在进行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.
我使用聚合框架对多个字段进行分组
{
_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)
我如何使用聚合框架来实现这一点。
这是我的收藏
{
"_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)我想为我的收藏中的每个用户获取具有最高价值的记录的评论。
//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) router.delete('/deletepost', (req, res) => {
// console.log(req.query.postid)
if (req.query.category === 'forsale') {
ForSalePosts.findById(req.query.postid)
// .then(post => console.log(post))
.deleteOne()
.catch(err => console.log(err))
AllPosts.updateOne({ user: req.query.userid },
{ $pull: { posts: { postid: req.query.postid } } })
.catch(err => console.log(err))
AllPosts.aggregate(
[
{ $match: { user: ObjectId(req.query.userid) } },
{ $unwind: '$posts' },
{ $sort: { 'posts.date': -1 } }
]
)
.then(posts => {
// console.log(posts)
res.json(posts)
})
.catch(err => res.status(404).json({ nopostfound: 'There is no posts' }))
}
})
Run Code Online (Sandbox Code Playgroud)
这是我的路线。我正在尝试删除文档中的一个项目。该项目正在被删除,但它返回旧值。例如 : …
我有一个看起来像这样的文件:
{
"_id": {
"$oid": "5b1586ccf0c56353e89d330b"
},
"address": {
"street": "123 Street",
"address2": "Address 2",
"city": "Some City",
"state": "MI",
"zip": "12345"
},
"subs": [
{
"invoices": [
{
"address": {
"street": "3061 Pine Ave SW",
"city": "Grandville",
"state": "AK",
"zip": "49418"
},
"lineItem": [
{
"images": [
{
"_id": {
"$oid": "5b1fca54e6ee1d80c463612d"
},
"name": "1528810066348_RSA Logo.jpeg",
"url": "https....",
"uploadDate": {
"$date": "2018-06-12T13:27:46.931Z"
},
"size": 91819
}
],
"_id": {
"$oid": "5b1fca54e6ee1d80c463612c"
},
"desc": "2",
"amt": 2
}
],
"_id": …Run Code Online (Sandbox Code Playgroud) 我有这个文件:
{
"_id" : ObjectId("5b673f525ef92ec6ef16504e"),
"events" : [
{
"name" : "Winner",
"map" : 0,
"something" : []
},
{
"name" : "Winner",
"map" : 2,
"something" : []
},
{
"name" : "DifferentName",
"map" : 2,
"something" : []
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我运行以下更新:
db.getCollection('test').updateOne({
"_id": ObjectId("5b673f525ef92ec6ef16504e"),
"events.name": "Winner",
"events.map": 2
},
{$push: {
"events.$.something": {
something: "test",
}
}
})
Run Code Online (Sandbox Code Playgroud)
我得到了不好的结果:
{
"_id" : ObjectId("5b673f525ef92ec6ef16504e"),
"events" : [
{
"name" : "Winner",
"map" : 0,
"something" : [
{ …Run Code Online (Sandbox Code Playgroud) 您好,我想通过查询排除一些字段。我正在使用 nodejs
public async getDoc() {
return new Promise((resolve, reject) => {
this.database.collection('users').find({email: "value3"}, {password: 0}).toArray((err, result) => {
if(err) {
reject(err)
}
resolve(result);
});
})
}
Run Code Online (Sandbox Code Playgroud)
但在结果集中我不断收到密码字段..
我有两个集合,如清单和任务。两个模式如下所示
清单架构将如下所示
{
"_id": "5b7d0f77e231b6b530b0ee5a",
"audit_checklist_type": "Weekly"
},
{
"_id": "5b7d3f33e7a57f38084efb09",
"audit_checklist_type": "Daily"
}
Run Code Online (Sandbox Code Playgroud)
任务架构如下所示
{
"_id": "5b7d65daf74be318e8378cf9",
"checklist_id": "5b7d3f33e7a57f38084efb09"
},
{
"_id": "5b7d662df74be318e8378cfb",
"checklist_id": "5b7d3f33e7a57f38084efb09"
}
Run Code Online (Sandbox Code Playgroud)
我的查询是
AuditChecklist.aggregate([
{ $match: {
$and: [
audit_checklist_type : "Daily"
]
} },
{
$lookup: {
from: 'AuditTask',
localField: '_id',
foreignField: 'checklist_id',
as: 'TaskData'
}
},
]).exec();
Run Code Online (Sandbox Code Playgroud)
我正在尝试获得类似的输出
{
"_id": "5b7d3f33e7a57f38084efb09",
TaskData": [
{
"_id": "5b7d65daf74be318e8378cf9",
"checklist_id": "5b7d3f33e7a57f38084efb09"
},
{
"_id": "5b7d662df74be318e8378cfb",
"checklist_id": "5b7d3f33e7a57f38084efb09"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但对我来说,由此产生的 TaskData 输出是空的。任何帮助是极大的赞赏。提前致谢!
mongodb ×10
mongoose ×3
node.js ×3
amazon-ec2 ×1
arrays ×1
database ×1
es6-promise ×1
javascript ×1
promise ×1