您好我是mongodb的新手,并尝试将具有不同类型(int)的对象转换为键值对.
我有这样的集合:
{
"_id" : ObjectId("5372a9fc0079285635db14d8"),
"type" : 1,
"stat" : "foobar"
},
{
"_id" : ObjectId("5372aa000079285635db14d9"),
"type" : 1,
"stat" : "foobar"
},
{
"_id" : ObjectId("5372aa010079285635db14da"),
"type" : 2,
"stat" : "foobar"
},{
"_id" : ObjectId("5372aa030079285635db14db"),
"type" : 3,
"stat" : "foobar"
}
Run Code Online (Sandbox Code Playgroud)
我想得到这样的结果:
{
"type1" : 2, "type2" : 1, "type3" : 1,
"stat" : "foobar"
}
Run Code Online (Sandbox Code Playgroud)
当前正在尝试聚合组,然后将类型值推送到数组
db.types.aggregate(
{$group : {
_id : "$stat",
types : {$push : "$type"}
}}
)
Run Code Online (Sandbox Code Playgroud)
但不知道如何将不同类型相加并将其转换为关键值
/* 0 */
{ …Run Code Online (Sandbox Code Playgroud) 我有一些文件存储在MongoDB中,类似于以下内容:
{
"id": "0001",
"name": "Joe",
"last_name": "Blogs",
"results":
[
{ "id": "5001", "mark": "78" },
{ "id": "5002", "mark": "105" },
{ "id": "5005", "mark": "97" },
]
}
Run Code Online (Sandbox Code Playgroud)
有许多类似于上面的条目.我遇到的问题是一些"结果"字段完全丢失,所以有些条目如下所示:
{
"id": "0001",
"name": "Joe",
"last_name": "Blogs"
}
Run Code Online (Sandbox Code Playgroud)
我试图聚合这个以返回"名称","last_name"和平均"标记".我对以下代码表示满意:
db.sites.aggregate(
{ $project : { name : "$name", last_name : "$last_name", results : "$results" } },
{ $unwind: "$results" },
{ $group : {
_id : "$_id",
average_mark : { $avg : "$results.mark" },
name : { $last : "$name" …Run Code Online (Sandbox Code Playgroud) 采集:
[
{ _id: "Foo", flag1: false, flag2: true, flag3: false },
{ _id: "Bar", flag1: true, flag2: false, flag3: true }
]
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否可以在聚合查询中调用方法?
aggregate({
$project: {
'_id': 1,
'status' MyService.getStatus($flag1, $flag2, $flag3)
}
});
Run Code Online (Sandbox Code Playgroud)
如果可能的话,它的语法是什么?结果:
[
{ _id: "Foo", status: 'ok' },
{ _id: "Bar", status: 'broken' }
]
Run Code Online (Sandbox Code Playgroud)
在我的实际应用程序中,每个文档有10个布尔标志。如果用户获得了此文档,我想转换这些标志并给它们一个含义(对于用户)。例如认为文件代表轮胎。
flag1 = true means tire have good pressure, false means low pressure
flag2 = true means depth of tire profile is good, false means little profile
and so on …Run Code Online (Sandbox Code Playgroud) 我的问题与此密切相关,但并不相似.
我的收藏中有一个示例文档:
db.t.insert({"a":1,"b":2});
Run Code Online (Sandbox Code Playgroud)
我的目的是投影一个名为combinedtype 的字段array,其值为both a和btogether.([1,2]).
我只是尝试与一个$project阶段聚合:
db.t.aggregate([
{$project:{"combined":[]}}
])
Run Code Online (Sandbox Code Playgroud)
MongoDB抛出一个错误:disallowed field type Array in object expression.
这意味着不能将字段投影为数组.
但是当我使用$cond运算符投影数组时,该字段会被投影.
db.t.aggregate([
{$project:{"combined":{$cond:[{$eq:[1,1]},["$a","$b"],"$a"]}}}
])
Run Code Online (Sandbox Code Playgroud)
我得到了o/p : {"combined" : [ "$a", "$b" ] }.
如果您注意到输出,则将a和的值b视为文字,而不是字段路径.
任何人都可以向我解释这种行为吗?当我让条件失败时,
db.t.aggregate([
{$project:{"combined":{$cond:[{$eq:[1,2]},["$a","$b"],"$a"]}}}
])
Run Code Online (Sandbox Code Playgroud)
我得到的预期输出$a被视为一个字段路径,因为$a它不是作为数组元素包含的.
我有一组具有以下结构的文档:
{
_id: 1,
array: [
{value: 10 },
{value: 11 },
{value: 12 }
]
}
Run Code Online (Sandbox Code Playgroud)
我想在集合上进行聚合查询:
获取每个项目的比例.(即,例如项目1的比例将是value项目1除以所有三个项目的值的总和.
注意:我想在一个查询中执行此操作.
我尝试在MongoDB中计算不同的值,但我没有得到我期望的结果.
以下是数据示例:
{
"_id" : ObjectId("55d354b0d5cec8aad571b8fe"),
"version" : "0.4",
"scan-time" : {
"start" : 1439913109,
"end" : 1439913136
},
"services" : [
{
"service-type" : "SV1",
"service-version" : "V1",
"location" : {
"ipv4-address" : "192.168.1.1",
"protocol" : "tcp"
},
"classification-method" : "probed"
},
{
"service-type" : "SV1",
"service-version" : "V2",
"location" : {
"ipv4-address" : "192.168.1.2",
"protocol" : "tcp"
},
"classification-method" : "probed"
},
{
"location" : {
"ipv4-address" : "192.168.1.3",
"protocol" : "tcp"
},
"classification-method" : "probed",
"service-type" : …Run Code Online (Sandbox Code Playgroud) 这些是我收藏的文件
{ "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extraMarks":10}
{ "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 }
{ "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 }
Run Code Online (Sandbox Code Playgroud)
只有文档1有一个字段额外标记:
现在我必须作为"最终"+"中期"+"额外标记"的总和进行投影
我写了一个投影查询如下:
db.students.aggregate([ { $project: { examTotal: …Run Code Online (Sandbox Code Playgroud) 我想对此架构执行查询:
{
"name" : "xxx",
"guests" : [
{
"category" : "category 1",
"arrived" : false
},
{
"category" : "category 2",
"arrived" : false
},
{
"category" : "category 1",
"arrived" : true
},
{
"category" : "category 2",
"arrived" : true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想得到一个特定的名字,一个列表,列出每个类别的客人百分比.
对于上面的文档作为示例,我想收到:
{
name : "xxx",
results : [
{
category : "category 1",
arrived : 50 (percent)
},
{
category : "category 2",
arrived : 50 (percent)
},
]
Run Code Online (Sandbox Code Playgroud)
有没有办法用一个MongoDB查询来做到这一点?另外,我应该在客户端还是服务器端进行此计算?
我有以下猫鼬模式:
EmployeeSchema:
var EmployeeSchema = new Schema({
name : String,
employeeDetailsId: {
type: Schema.Types.ObjectId,
ref: 'employeedetails'
}
});
Run Code Online (Sandbox Code Playgroud)
EmployeeDetailSchema:
var EmployeeDetailSchema = new Schema({
employeeId: {
type: Schema.Types.ObjectId,
ref: 'employee'
},
statusId: {
type: Schema.Types.ObjectId,
ref: 'status'
},
primarySkills: [
{
type: Schema.Types.ObjectId,
ref: 'skills'
}]
});
Run Code Online (Sandbox Code Playgroud)
技能图式:
var SkillsSchema = new Schema({
name: {
type: String,
required: true
}
});
Run Code Online (Sandbox Code Playgroud)
以下是保存在EmployeeDetails集合中的数据:
/* 1 */
{
"_id" : ObjectId("583fbbfe78854dd424f0523f"),
"employeeId" : ObjectId("583f114e1cff44b7ab414dc1"),
"statusId" : ObjectId("583ee05a1d5161941632091a"),
"secondarySkills" : [], …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
假设我有两个Mongoose集合- conversation和message-,并且我想显示特定用户所在的对话列表,按最新消息排序,并在对话名称下方显示该消息的预览
在获得用户的对话之后,如何才能仅从每个对话中选择最新消息,并将这些消息附加到其相应的对话中?(鉴于架构看起来像这样):
var ConversationSchema = new Schema({
name: String,
participants: {
type: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
}
});
var MessageSchema = new Schema({
conversation: {type: Schema.Types.ObjectId, ref: 'Conversation', required: true},
text: {type: String, required: true},
user: {type: Schema.Types.ObjectId, ref: 'User', required: true}
});
Run Code Online (Sandbox Code Playgroud)
我收到的消息是我可能应该使用Mongo的“聚合”框架,但是我以前从未使用过它,因此我需要一些帮助。谢谢!
mongoose mongodb node.js mongodb-query aggregation-framework