这是查询
[
{
"$project": {
"formattedDate": {
"$dateToString": { "format": "%Y-%m-%d", "date": "$ceatedAt" }
},
"createdAtMonth": { "$month": "$ceatedAt" },
"rating": 1
}
},
{
"$group": {
"_id": "$formattedDate",
"average": { "$avg": "$rating" },
"month": { "$first": "$createdAtMonth" },
}
}
]
Run Code Online (Sandbox Code Playgroud)
我需要时间戳中的日期.怎么做?
我有像{dob:"02-23-2000"}数据库中的dob 字段。现在我想在字符串格式的 dob 字段上执行$gte和<e如下所示:
db.panelists.count({"dob":{ '$gte': '08-02-1998', '$lte': '08-02-2003' }});
Run Code Online (Sandbox Code Playgroud)
我总是将计数值设为零。
任何人都可以帮助我以相同的 dob 格式解决此查询。
我的 mongoDB 集合中有如下数据。我想在今天的基础上计算总数或search_term。
_id:ObjectId("5b7e1d38981cdc1a7c8bb5fc")
search_term:"Baiyoke Boutique"
date:"August 23rd 2018, 9:34:32 am"
_id:ObjectId("5b7e1fa8fa27fd2754080ad9")
search_term:"Baiyoke Boutique"
date:"August 23rd 2018, 9:44:56 am"
_id:ObjectId("5b7e28314d2d0f388c1596cd")
search_term:"Baiyoke Florting Market"
date:"August 23rd 2018, 10:21:21 am"
Run Code Online (Sandbox Code Playgroud)
我试过以下查询。我已经使用了时刻。我不是我犯了什么错误。
var start = moment().startOf('day');
var end = moment().endOf('day');
history.find({
date: {
$gte: start,
$lt: end
}
}).count().toArray(
function (e, res) {
if (e) callback(e)
else callback(null, res)
});
Run Code Online (Sandbox Code Playgroud) 我在nodejs(express)中使用mongodb作为数据库的一个项目非常难.当我使用sort()获取所有数据时,它以错误的方式返回数据,所以有没有办法正确格式化,因为我期望如下:如果我们在DB中有三条记录:
---------------------
id | Name | aga
---------------------
1 | atul | 21
---------------------
2 | Bhavik | 22
---------------------
3 | Jay | 25
Run Code Online (Sandbox Code Playgroud)
我目前得到的是:
2,3,1系列数据
我期待的是:1,2,3
这意味着忽略这种情况,而排序是可能的,而无需添加新列.
我有一个名为“会议”的 mongoDB 集合,其中包含一组参与者,如下所示:
[
{
"_id" : 5b894357a0c84d5a5d221f25,
"conferenceName" : "myFirstConference",
"startDate" : 1535722327,
"endDate" : 1535722420,
"participants" : [
{
"name" : "user1",
"origin" : "internal",
"ip" : "192.168.0.2"
},
{
"name" : "user2",
"origin" : "external",
"ip" : "172.20.0.3"
},
]
},
...
]
Run Code Online (Sandbox Code Playgroud)
我想得到以下结果:
[
{
"conferenceName" : "myFirstConference",
"startDate" : 1535722327,
"endDate" : 1535722420,
"internalUsersCount" : 1
"externalUsersCount" : 1,
},
...
]
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的请求,但它不起作用:
db.getCollection("conference").aggregate([
{
$addFields: {
internalUsersCount : {
$size : { "$participants" : …Run Code Online (Sandbox Code Playgroud) 我是 MongoDB 的新手,所以我不确定我是否正确地表达了我的问题。
我有一个集合,其中的数据如下所示:
{
"_id" : ObjectId("66666"),
"Id" : 994,
"PostType" : 1,
"AnswerId" : 334,
"CreationDate" : ISODate("1994-09-05T09:34:36.177+0000"),
"Tags" : "test",
"Parent" : null,
}
{
"_id" : ObjectId("8888"),
"Id" : 334,
"PostTypeId" : 2,
"AnswerId" : NaN,
"CreationDate" : ISODate("20005-08-03T11:29:42.880+0000"),
"ParentId" : 994
}
Run Code Online (Sandbox Code Playgroud)
两个文档都在同一个集合中,我尝试使用 PostType:1 文档中的 AnswerId,并使用该值作为主 ID 来提取其 CreationDate。
这是我试图实现的逻辑,但它似乎不起作用:
db.collection.aggregate([
{$project:{_id:"$Id", Title:"$Title", Answerid:"$AnswerId", QuestionDate:"$CreationDate"}},
{$match:{Id:"$Answerid"}},
{$project:{AnswerDate:"$CreationDate"}}
])
Run Code Online (Sandbox Code Playgroud)
我愿意接受任何建议。
mongodb mongo-shell mongodb-query nosql-aggregation aggregation-framework
我是 mongodb 的初学者,我正在尝试在 node.js 应用程序中使用 mongoose 编写查询:
const objectIdValue = secondId.valueOf();
const existedRelation = await this.model.aggregate([
{ $match: { _id: firstId } },
{ $project: {
relations: {
$filter: {
input: '$links',
as: 'link',
cond: {
$and: [
{ $eq: ['$$link.target.entityId', `${objectIdValue}`] },
{ $eq: ['$$link.linkTypeId', linkTypeId] },
],
},
},
},
},
},
]);
console.log('existedRelation: ', existedRelation);
Run Code Online (Sandbox Code Playgroud)
当我执行它时,我得到了这个结果:
existedRelation: []
Run Code Online (Sandbox Code Playgroud)
我尝试使用 mongoShell 执行它:
db.tasks.aggregate([
{ $match:{ _id: ObjectId("5bbf5800be37394f38a9727e") }},
{
$project: {
relations:{
$filter:{
input: '$links',
as: "link",
cond: …Run Code Online (Sandbox Code Playgroud) 我需要从 mongodb 中存在的单个文档创建一些图。我只能使用 mongodb 聚合框架(所以例如我不能将文档拉入 python 并在那里使用它)。我正在使用元数据库的查询生成器,因此我在这方面受到限制。
为了做到这一点,我首先使用一些$match查询来识别我需要查看的文档(这些文档是预定义的和静态的)。该$match阶段结束后,我留下了一份具有以下结构的文档(这没问题)。
{
"id": 1,
"locs": {
"a":1,
"b":2,
"c":3
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将此结构更改为如下所示:
[{"a":1}, {"b":2}, {"c":3"}]
或任何其他允许我从结构中创建饼图的形式。
谢谢!
我在 MongoDB 3.6 中有两个集合:
users: [
{name: "John", allowedRoles: [1, 2, 3]},
{name: "Charles", allowedRoles: [1]},
{name: "Sarah", isAdmin: true}
]
roles: [
{_id: 1, name: "Foo", description: "This role allows to foo the blargs"},
{_id: 2, name: "Bar", description: "..."},
{_id: 3, name: "Doh", descripcion: "..."}
]
Run Code Online (Sandbox Code Playgroud)
我对 MongoDB 很陌生;我刚刚想出了如何使用聚合阶段查询用户并从他的角色中加入所有数据$lookup:
db.users.aggregate([{
"$match": { "name": "John" } // Or without this $match part, to get all users
},{ //
"$lookup": {
"from": "roles",
"localField": "allowedRoles",
"foreignField": …Run Code Online (Sandbox Code Playgroud) 我正在尝试进行一些聚合,但遇到以下问题。我需要使用“管道”,当我正在寻找的数组丢失时,我收到一个错误。
{
$lookup: {
from: 'comments',
let: { comments: '$comments' },
pipeline: [
{
$match: {
$expr: {
$in: ['$_id', '$$comments']
},
isDeleted: false
}
}
],
as: 'comments'
}
}
Run Code Online (Sandbox Code Playgroud)
在这个阶段,我收到以下错误:
'$in requires an array as a second argument, found: missing'
因为并非所有文档都有“评论”字段。
注意:我使用管道而不是foreingField和localField ,因为我需要使用isDeleted: false其他匹配条件进行过滤。
无论如何,仅当文档具有字段注释时才进行此查找?
谢谢!