我正在做 mongodb 与 mysql 的比较,并将 mysql 数据导入到 mongodb 集合中(> 500000 条记录)。该集合如下所示:
{
"_id" : ObjectId(""),
"idSequence" : ,
"TestNumber" : ,
"TestName" : "",
"S1" : ,
"S2" : ,
"Slottxt" : "",
"DUT" : ,
"DUTtxt" : "",
"DUTver" : "",
"Voltage" : ,
"Temperature" : ,
"Rate" : ,
"ParamX" : "",
"ParamY" : "",
"Result" : ,
"TimeStart" : new Date(""),
"TimeStop" : new Date(""),
"Operator" : "",
"ErrorNumber" : ,
"ErrorText" : "",
"Comments" : "", …Run Code Online (Sandbox Code Playgroud) mysql performance mongodb mongodb-query aggregation-framework
几乎是MongoDB聚合框架的新手.我想知道是否有办法使用动态变量在聚合框架中执行项目.
例如.这是我的代码,它不是javascript语法,但如果你明白了这一点:
// My javascript variable
var my_variable = "salary";
//
db.article.aggregate(
{ $project : {
title : 1 ,
author : 1 ,
}});
Run Code Online (Sandbox Code Playgroud)
现在,在上面的代码中,我想动态地将"author"的投影替换为"salary".对于这样的事情
db.article.aggregate(
{ $project : {
title : 1 ,
"my_variable" : 1 ,
}});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,将动态投影薪水.
考虑以下集合,
{
"_id": ObjectId("545c535e75de3e630c8b4567"),
"tables" : [
{
"_id" : ObjectId("54584f5975de3e040fe97319"),
"title" : "001",
},
{
"_id" : ObjectId("54584fb175de3e1c0fe97319"),
"title" : "002",
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要从这个集合中检索table.title ="001"的数据.我使用了laravel框架.我尝试了以下代码,但没有正常工作.如果有人有想法解决这个问题,请帮助我.
DB::connection($this->connection)->collection($this->collection)->where('tables','elemMatch',array('title'=>"001"))->get();
DB::connection($this->connection)->collection($this->collection)->where('tables.title',"001")->get();
DB::connection($this->connection)->collection($this->collection)->where('tables.$.title',"001")->get();
Run Code Online (Sandbox Code Playgroud) 我在 mongo db 的集合中有以下类型的文档
{_id:xx,
Run Code Online (Sandbox Code Playgroud)iddoc:yy, type1:"sometype1", type2:"sometype2", date: { year:2015, month:4, day:29, type:"day" }, count:23 }
我想对所有文档按 iddoc 分组的字段计数求和,其中:
type1 in ["type1A","type1B",...] where type2 in ["type2A","type2B",...] date.year: 2015, date.month: 4, date.type: "day" date.day 介于 4 和 7 之间
然后我想对这些总和进行排序。
我现在知道怎么做(见这个问题)
db.test.aggregate([
// Filter the docs based on your criteria
{$match: {
type1: {$in: ['type1A', 'type1B']},
type2: {$in: ['type2A', 'type2B']},
'date.year': 2015,
'date.month': 4,
'date.type': 'day',
'date.day': {$gte: 4, $lte: 7}
}},
// Group by iddoc and count …Run Code Online (Sandbox Code Playgroud) 馆藏结构
Order = new Schema
index: { type: Number, unique: true }
number: Date
status: { type: String, enum: ['success', 'failure'] }
created_at: { type: Date, default: Date.now }
updated_at: { type: Date, default: Date.now }
Run Code Online (Sandbox Code Playgroud)
我需要有关查询的帮助,该查询返回一组对象,这些对象的数据为按日期分组的成功计数和失败计数。前任-
orders = {
28-10-2016:{
success_count: 10,
failure_count: 10
},
29-10-2016: {
success_count: 10,
failure_count: 10
}
}
Run Code Online (Sandbox Code Playgroud) database mongoose mongodb mongodb-query aggregation-framework
我必须进行查询,因此在查询中我必须仅在大小为1时显示。在我的投影中,我显示结果,并且我有查询,但是仅显示所有大小,但是我只需要显示大小为1时,我不知道如何制作。我用了一个投影。这是一个文档: 文档
我的查询是这样的:
db.movies.aggregate([
{"$project":
{
"_id": 0,
"longi":{$size:{ $split: [ "$title", " " ]}}
}
}]);
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
{ "longi" : 2 }
{ "longi" : 2 }
{ "longi" : 6 }
{ "longi" : 11 }
{ "longi" : 2 }
{ "longi" : 2 }
{ "longi" : 6 }
{ "longi" : 2 }
{ "longi" : 1 }
{ "longi" : 4 }
{ "longi" : 5 }
{ "longi" : 3 }
{ "longi" …Run Code Online (Sandbox Code Playgroud) 我有这个文件:
{
"_id": "59b804e1ee8a4071a5ea3fcc",
"description" : "description",
"imagepath" : "https://example.com",
"type" : "label",
"downvotes" : -25,
"upvotes" : 15,
"language" : "en",
"approoved" : true
},
{
"_id": "59b804e1ee8a4071a5ea4dfd",
"description" : "description",
"imagepath" : "https://example.com",
"type" : "label",
"downvotes" : 0,
"upvotes" : 30,
"language" : "en",
"approoved" : true
}
Run Code Online (Sandbox Code Playgroud)
如何按upvotes&downvotes值的总和对这 2 个对象进行排序?
(带有 id 的对象59b804e1ee8a4071a5ea4dfd应该放在最前面,依此类推)
我试图聚合
{"$group" : {
_id:{upvotes:"$upvotes",downvotes:"$downvotes"},
count:{$sum:1}
}}
Run Code Online (Sandbox Code Playgroud)
但它将所有类似的文档归为一组。我无法弄清楚聚合过程/语法。
编辑:根据 Veeram 的回答,这里是使用猫鼬的工作代码,以防万一有人掉线:
Labels.aggregate([
{"$addFields":{ "sort_order":{"$add":["$upvotes", "$downvotes"]}}},
{"$sort":{"sort_order":-1}}, …Run Code Online (Sandbox Code Playgroud) 字段被添加但随后消失。这是 mongo shell 中的代码:
> db.users.aggregate([{$addFields:{totalAge:{$sum:"$age"}}}])
{ "_id" : ObjectId("5acb81b53306361018814849"), "name" : "A", "age" : 1, "totalAge" : 1 }
{ "_id" : ObjectId("5acb81b5330636101881484a"), "name" : "B", "age" : 2, "totalAge" : 2 }
{ "_id" : ObjectId("5acb81b5330636101881484b"), "name" : "C", "age" : 3, "totalAge" : 3 }
> db.users.find().pretty()
{ "_id" : ObjectId("5acb81b53306361018814849"), "name" : "A", "age" : 1 }
{ "_id" : ObjectId("5acb81b5330636101881484a"), "name" : "B", "age" : 2 }
{ "_id" : ObjectId("5acb81b5330636101881484b"), "name" : "C", "age" …Run Code Online (Sandbox Code Playgroud) 我正在为 mongodb 中的基本查找而苦苦挣扎。尝试多种组合后,我无法弄清楚出了什么问题。我看到有很多关于 SO 解决它的问题,但在尝试所有答案后没有任何效果。
这是我的用户集合
这是我的物品收藏
每个项目都有一个所有者,它映射到用户集合中用户的 _id。对于我的查询,我试图通过相应的用户获取项目。这是查询
db.items.aggregate([
{
"$lookup" : {
localField : "owner",
from : "users",
foreignField : "_id",
as : "users"
}
}
])
Run Code Online (Sandbox Code Playgroud)
它返回这个 -
我尝试了不同的变化 - 例如
db.items.aggregate([
{
"$lookup" : {
localField : "owner.str",
from : "users",
foreignField : "_id.str",
as : "users"
}
}
])
Run Code Online (Sandbox Code Playgroud)
这导致用户数组填充不正确(所有用户!)。
我究竟做错了什么?
编辑
这是项目集合
{
"_id": {
"$oid": "5b268395c176db1548bd92c2"
},
"title": "Item #1",
"description": "Item 1 Description",
"categories": [
{ …Run Code Online (Sandbox Code Playgroud) 我有一个对象数组,其中包含未知数量的数组元素:
{ "content": [
{
"_id": "refbooks",
"total": 189,
"published": 189,
"created": 0,
"approved": 0,
"rejected": 0,
"sent_for_approval": 0
},
{
"_id": "weblinks",
"total": 1911,
"published": 1899,
"created": 10,
"approved": 2,
"rejected": 0,
"sent_for_approval": 0
},.................]}
Run Code Online (Sandbox Code Playgroud)
我想将其转换为给定的对象:
{ "content": {
{
"_id": "refbooks",
"total": 189,
"published": 189,
"created": 0,
"approved": 0,
"rejected": 0,
"sent_for_approval": 0
},
{
"_id": "weblinks",
"total": 1911,
"published": 1899,
"created": 10,
"approved": 2,
"rejected": 0,
"sent_for_approval": 0
},.................}}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 $unwind,但没有得到所需的输出。如何在 MongoDB 中实现这个内部聚合管道?
我正在使用 Mongo 3.4 版。