我试图使用聚合框架(使用ruby)并像这样投影日期:
db['requests'].aggregate([
{"$project" => {
_id: 0,
method: '$method',
user: '$user',
year: {'$year' => '$timestamp'}
}}])
Run Code Online (Sandbox Code Playgroud)
该文件就像这样:
{
_id: ObjectId("5177d7d7df26358289da7dfd"),
timestamp: ISODate("2013-04-12T03:58:05+00:00"),
method: "POST",
status: "200",
inputsize: "874",
outputsize: "4981",
user: "131"
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Mongo::OperationFailure: Database command 'aggregate' failed: (errmsg: 'exception: can't convert from BSON type EOO to Date'; code: '16006'; ok: '0.0').
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为如果我在与mongorestore导入的完全相同的数据库上运行它,它可以正常工作.
假设我有这些字段的集合:
{
"category" : "ONE",
"data": [
{
"regex": "/^[0-9]{2}$/",
"type" : "TYPE1"
},
{
"regex": "/^[a-z]{3}$/",
"type" : "TYPE2"
}
// etc
]
}
Run Code Online (Sandbox Code Playgroud)
所以我的输入是"abc"所以我想获得相应的类型(或最佳匹配,尽管最初我假设RegExes是独占的).有没有可能通过良好的性能实现这一目标?(这将排除迭代RegEx数组的每个项目)
请注意,如果可能,可以重新安排架构,因为该项目仍处于设计阶段.所以替代方案将受到欢迎.
每个类别可以有大约100 - 150个RegExes.我打算有大约300个类别.但我知道这些类型是相互排斥的.
一个类别的真实世界示例:
type1=^34[0-9]{4}$,
type2=^54[0-9]{4}$,
type3=^39[0-9]{4}$,
type4=^1[5-9]{2}$,
type5=^2[4-9]{2,3}$
Run Code Online (Sandbox Code Playgroud) 我有一个类似的mongo文件
{ "_id" : 12, "location" : [ "Kannur","Hyderabad","Chennai","Bengaluru"] }
{ "_id" : 13, "location" : [ "Hyderabad","Chennai","Mysore","Ballary"] }
Run Code Online (Sandbox Code Playgroud)
从这里我如何获得位置聚合(不同的区域计数).就像是
Hyderabad 2,
Kannur 1,
Chennai 2,
Bengaluru 1,
Mysore 1,
Ballary 1
Run Code Online (Sandbox Code Playgroud) 最近我已经从mongosharp 1.8迁移到2.0.我遇到的唯一问题是聚合日期字段.让我告诉你如何构造查询:
var aggregateResult = Items.Aggregate()
.Group(
g => new {
// some fields
Day = g.DateTime.DayOfYear
},
z => new {
MyKey = z.Key
// agrregation functions
})
.Project(
d => new {
// projection for other fields
d.MyKey.Day
});
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:No matching creator found.我已检查生成的查询并手动执行它 - 结果很完美.再现测试代码并与我比较我发现问题是在日期.那么,有人能指出我更正日期的语法/查询规则吗?下面生成的查询证明查询是正确的.
aggregate(
[
{
"$group" : {
"_id" : {
"Day" : {
"$dayOfYear" : "$DateTime"
}
},
}
},
{
"$project" : {
"Day" : "$_id.Day", …Run Code Online (Sandbox Code Playgroud) 我希望通过拥有$或者财产来获得$ cond的$ sum:
db.collectionName.aggregate(
{
"$group": {
"_id":'$created_at',
"count": {"$sum": 1},
"count_failure": {
"$sum": {
"$cond": [
{
"$id":
{ "$in": [ 0,100,101,102,103,104,105 ] }
},
1,
0
]
}
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
但错误说: Invalid operator "$id"
语法有什么问题?或者我错误地写了查询.
目前我通过以下方式实现此目标
db.collectionName.aggregate(
{
"$group": {
"_id":'$created_at',
"count": {"$sum": 1},
"count_failure": {
"$sum": {
"$cond": [
{
"$or":[
{ "$eq": [ "$id", 0 ] },
{ "$eq": [ "$id", 100 ]},
{ "$eq": [ "$id", 101 ]},
{ "$eq": [ "$id", 102 …Run Code Online (Sandbox Code Playgroud) 我有一个包含多个集合的数据库(整体约15mil文档),文档看起来像这样(简化):
{'Text': 'blabla', 'ID': 101}
{'Text': 'Whuppppyyy', 'ID': 102}
{'Text': 'Abrakadabraaa', 'ID': 103}
{'Text': 'olalalaal', 'ID': 104}
{'Text': 'test1234545', 'ID': 104}
{'Text': 'whapwhapwhap', 'ID': 104}
Run Code Online (Sandbox Code Playgroud)
它们都有一个唯一的_id字段,但我想删除与另一个字段(外部ID字段)相对应的重复项.
首先,我尝试了一种非常手动的方法,然后使用列表和删除,但数据库看起来太大,需要很长时间并且不实用.
其次,以下版本不再适用于当前的MongoDB版本,即使有人提出建议.
db.collection.ensureIndex( { ID: 1 }, { unique: true, dropDups: true } )
Run Code Online (Sandbox Code Playgroud)
所以,现在我正在尝试创建一个map reduce解决方案,但我真的不知道我在做什么,特别是在使用另一个字段(不是数据库_id)来查找和删除重复项时遇到困难.这是我糟糕的第一种方法(从一些互联网来源采用):
map = Code("function(){ if(this.fieldName){emit(this.fieldName,1);}}")
reduce = Code("function(key,values) {return Array.sum(values);}")
res = coll.map_reduce(map,reduce,"my_results");
response = []
for doc in res.find():
if(doc['value'] > 1):
count = int(doc['value']) - 1
docs = col.find({"fieldName":doc['ID']},{'ID':1}).limit(count)
for i in docs:
response.append(i['ID'])
coll.remove({"ID": {"$in": response}}) …Run Code Online (Sandbox Code Playgroud) Rails 4.2.5, Mongoid 5.1.0
我有三个型号- Mailbox,Communication和Message.
mailbox.rb
class Mailbox
include Mongoid::Document
belongs_to :user
has_many :communications
end
Run Code Online (Sandbox Code Playgroud)
communication.rb
class Communication
include Mongoid::Document
include Mongoid::Timestamps
include AASM
belongs_to :mailbox
has_and_belongs_to_many :messages, autosave: true
field :read_at, type: DateTime
field :box, type: String
field :touched_at, type: DateTime
field :import_thread_id, type: Integer
scope :inbox, -> { where(:box => 'inbox') }
end
Run Code Online (Sandbox Code Playgroud)
message.rb
class Message
include Mongoid::Document
include Mongoid::Timestamps
attr_accessor :communication_id
has_and_belongs_to_many :communications, autosave: true
belongs_to :from_user, class_name: 'User'
belongs_to …Run Code Online (Sandbox Code Playgroud) ruby-on-rails mongodb mongoid mongodb-query aggregation-framework
我们试图将一个字符串数组'连接'到聚合内的单个字符串.
给出以下数据集:
收集1:
{
id: 1234,
field: 'test'
}
Run Code Online (Sandbox Code Playgroud)
收集2:
{
id: 1111,
collection1_id: 1234,
name: 'Max'
},
{
id: 1112,
collection1_id: 1234,
name: 'Andy'
}
Run Code Online (Sandbox Code Playgroud)
当前结果(查找后等):
{
id: 1234,
field: 'test',
collection2: ['Max', 'Andy']
}
Run Code Online (Sandbox Code Playgroud)
期望的结果:
{
id: 1234,
field: 'test',
collection2: 'Max, Andy'
}
Run Code Online (Sandbox Code Playgroud)
是否有可能将'collection2'加入单个字符串?我们尝试过,$concat但它只接受字符串.
在MongoDB中,我需要能够在主文档内的数组内的文档中展开嵌套数组.
{
"_id" : ObjectId("5808d700536d1a3d69f4cf51"),
"last_name" : "Maity",
"xiith_mark" : 58,
"id" : "3539488",
"first_name" : "Harshavardhan",
"course_name" : "BE/B.Tech",
"institute_name_string" : "Abhayapuri College, P.O. Abhayapuri",
"profile_percentage" : 45,
"xiith_mark_type" : "Percentage",
"xth_mark_type" : "Percentage",
"date_of_birth" : "14-April-1993",
"xth_mark" : 30,
"last_login" : 1470827224,
"percentage" : 55,
"job_details" : [
{
"status" : NumberLong(6),
"applied_date" : NumberLong(1470831441),
"job_id" : NumberLong(92928),
"contact_viwed_status" : 0,
"label_name" : [
"shortlisted",
"rejected"
],
"questionnaire_status" : 0,
"batch_id" : NumberLong(6),
"call_letter" : NumberLong(812)
},
{ …Run Code Online (Sandbox Code Playgroud) MongoError:无法识别的管道阶段名称:'$ addFields'."mongoose":"^ 4.5.8"我的源代码:
Post.aggregate(
[{
$addFields: {
userName: { $concat: [ "$author.firstName", " ", "$author.lastName" ] }
}
//$project: { userName: { $concat: [ "$author.firstName", " ", "$author.lastName" ] } } //this is ok!
}],
function (err, result) {
if (err) {
console.log(err);
return;
}
console.log(result);
}
)
Run Code Online (Sandbox Code Playgroud)
发布模型:
let schema = {
id: "post",
properties: {
content: {type: "string"},
author: {
type: "object",
id: {type: "string"},
avatar: {type: "string"},
firstName: {type: "string"},
lastName: {type: "string"},
status: {type: "string"}
}, …Run Code Online (Sandbox Code Playgroud)