我有多个类似这样的数据
{
"_id" : ObjectId("57189fcd72b6e0480ed7a0a9"),
"venueId" : ObjectId("56ce9ead08daba400d14edc9"),
"companyId" : ObjectId("56e7d62ecc0b8fc812b2aac5"),
"cardTypeId" : ObjectId("56cea8acd82cd11004ee67a9"),
"matchData" : [
{
"matchId" : ObjectId("57175c25561d87001e666d12"),
"matchDate" : ISODate("2016-04-08T18:30:00.000Z"),
"matchTime" : "20:00:00",
"_id" : ObjectId("57189fcd72b6e0480ed7a0ab"),
"active" : 3,
"cancelled" : 0,
"produced" : 3
},
{
"matchId" : ObjectId("57175c25561d87001e666d13"),
"matchDate" : ISODate("2016-04-09T18:30:00.000Z"),
"matchTime" : "20:00:00",
"_id" : ObjectId("57189fcd72b6e0480ed7a0aa"),
"active" : null,
"cancelled" : null,
"produced" : null
}
],
"__v" : 0
}
Run Code Online (Sandbox Code Playgroud)
我正在做分组companyId并且它的工作很好但是我想matchData 根据matchtime和matchId为此目的进行搜索,我$unwind matchData在放松之后我使用这样的搜索查询 …
我有一个包含两个文档的集合
{
"_id" : ObjectId("5784def78c669ee061519aaf"),
"name" : "nithin",
"sal" : 2000.0,
"age" : 20.0
},
{
"_id" : ObjectId("5784def78c669ee061519ab0"),
"name" : "nithin",
"sal" : 20.0,
"age" : 2000.0
}
Run Code Online (Sandbox Code Playgroud)
我想找到给定名字和相应年龄的最小萨尔
在这种情况下,最小 Sal 为 20,相应的年龄为 2000
运行我的查询如下:
db.test.aggregate([{
"$group":{"_id":"$name" ,"minSalary" :{"$min":"$sal"} , "ages":{"$addToSet":{"ageval":"$age","salval":"$sal"}}}}
,{"$project": {
"_id":0,
"sal":"$minSalary",
"ages": "$ages"
}
},
{
"$unwind":"$ages"
}
,
{
**"$match":{ "ages.salval":**20** }**
}
]
)
Run Code Online (Sandbox Code Playgroud)
但问题是我希望 $match 直接应用于 minSalary 而不是将过滤器值硬编码为 20 。请帮忙
我使用节点在 mongoDB 中创建了类别,为每个插入添加了父属性以确定哪个是父类别。
[{ _id: 5889a06f274afdfd6f2bf249,
cat_id: 1,
title: 'Parent Category',
description: '',
parent: null,
status: 1}]
[{ _id: 5889a06f274afdfd6f2bf249,
cat_id: 1,
title: 'Child Category',
description: '',
parent: 1,
status: 1}]
Run Code Online (Sandbox Code Playgroud)
现在,当我使用 mongoDB 的 db.find() 方法查询时,输出 json 应该使用 node。
{
obj: [
{
_id: "5889a06f274afdfd6f2bf249",
cat_id: 1,
title: "Parent Category",
description: "",
parent: null,
status: 1,
subcategories: [
{
_id: "5889a06f274afdfd6f2bf249",
cat_id: 1,
title: "Child Category",
description: "",
parent: 1,
status: 1
}
]
}
]}
Run Code Online (Sandbox Code Playgroud) 我是猫鼬的新手。我有这样的模型。
var ForumSchema = new mongoose.Schema({
User_id : {type : Schema.Types.ObjectId, ref : 'User'},
Title : {type : String},
Content : {type : String},
Tags : [{type : String}],
isPublic : {type : Boolean, default : false},
Vote : [{
User_id : {type : Schema.Types.ObjectId, ref : 'User'},
Kind : {type : Boolean}
}],
Rate : [{
User_id : {type : Schema.Types.ObjectId, ref : 'User'},
Count : {type : Number}
}],
Comment : [{
User_id : {type : Schema.Types.ObjectId, ref …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
我有一个这样的收藏
[{
"_id" : ObjectId("590c2331dc1a05e7d3525e70"),
"name" : "Orange",
"quarters" : {
"first" : 20,
"middle" : 53,
"last" : 52
}
}, {
"_id" : ObjectId("590c2405dc1a05e7d3525e71"),
"name" : "Apple",
"quarters" : {
"first" : 42,
"middle" : 37,
"last" : 16
}
}]
Run Code Online (Sandbox Code Playgroud)
我想要这样的结果
[{
name: "Orange",
first: 20
},{
name: "Orange",
middle: 53
},{
name: "Orange",
last: 52
},{
name: "Apple",
first: 42
},{
name: "Apple",
middle: 37
},{
name: "Apple",
last: 16
}]
Run Code Online (Sandbox Code Playgroud)
我试图找到解决方案,但发现很少有与此问题类似的问题,但没有运气
提前致谢
我正在尝试获取数组内特定字段的总和。问题是,该数组也在另一个数组中。我的文件如下(简化):
{
"nome": "test-1"
"notas": [{
"numero_fiscal": "0001",
"valores": {
"portal": 1000,
"arquivo": 1000
},
"abatimentos": [{
"valor": 250,
"tipo": "TIPO 1"
}, {
"valor": 250,
"tipo": "TIPO 2"
}]
}, {
"numero_fiscal": "0002",
"valores": {
"portal": 2000,
"arquivo": 2000
},
"abatimentos": [{
"valor": 500,
"tipo": "TIPO 1"
}, {
"valor": 500,
"tipo": "TIPO 2"
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
我希望我的输出是这样的:
{
"_id": "resumo",
"total_notas": 2, // this is the counter for the array "notas"
"soma_portal": 3000, // this is the sum …Run Code Online (Sandbox Code Playgroud) 我的收藏中有以下文件。每个文档都包含有关特定位置的历史天气数据:
{
'location':'new york',
'history':[
{'timestamp':1524542400, 'temp':79, 'wind_speed':1, 'wind_direction':'SW'}
{'timestamp':1524548400, 'temp':80, 'wind_speed':2, 'wind_direction':'SW'}
{'timestamp':1524554400, 'temp':82, 'wind_speed':3, 'wind_direction':'S'}
{'timestamp':1524560400, 'temp':78, 'wind_speed':4, 'wind_direction':'S'}
]
},
{
'location':'san francisco',
'history':[
{'timestamp':1524542400, 'temp':80, 'wind_speed':5, 'wind_direction':'SW'}
{'timestamp':1524548400, 'temp':81, 'wind_speed':6, 'wind_direction':'SW'}
{'timestamp':1524554400, 'temp':82, 'wind_speed':7, 'wind_direction':'S'}
{'timestamp':1524560400, 'temp':73, 'wind_speed':8, 'wind_direction':'S'}
]
},
{
'location':'miami',
'history':[
{'timestamp':1524542400, 'temp':84, 'wind_speed':9, 'wind_direction':'SW'}
{'timestamp':1524548400, 'temp':85, 'wind_speed':10, 'wind_direction':'SW'}
{'timestamp':1524554400, 'temp':86, 'wind_speed':11, 'wind_direction':'S'}
{'timestamp':1524560400, 'temp':87, 'wind_speed':12, 'wind_direction':'S'}
]
}
Run Code Online (Sandbox Code Playgroud)
我想获取每个位置(或多或少)的最新天气数据列表,如下所示:
{
'location':'new york',
'history':{'timestamp':1524560400, 'temp':78, 'wind_speed':4, 'wind_direction':'S'}
},
{
'location':'san francisco',
'history':{'timestamp':1524560400, 'temp':73, …Run Code Online (Sandbox Code Playgroud) 我有 2 个集合:
用户
{
id: 1,
name: "Michael",
starred: [1, 2]
}
Run Code Online (Sandbox Code Playgroud)
学校
{
id: 1,
name: "Uni",
faculties: [{
id:1000,
name: "faculty1",
subjects: [
{id: 1, name: "sub1"},
{id: 2, name: "sub2"},
{id: 3, name: "sub3"}
]
}]
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的用户集合中,我想查找并收集每个主题对象,其 ID 在已加星标中找到。IE。starred: [1,2]包含id我想要的主题。
所以最终结果应该返回
[{id: 1, name: sub1},{id: 2, name: sub2}]
Run Code Online (Sandbox Code Playgroud)
我目前正在使用 whis 聚合管道
{$match: {name: 'Michael'}},
{$unwind: "$faculties"},
{$unwind: "$faculties.subjects"},
{$lookup:
{
from: 'schools',
localField: 'starred',
foreignField: 'faculties.subjects.id',
as: 'starredSubjects'
}
},
{$project: …Run Code Online (Sandbox Code Playgroud) 我有一个包含 100 万个文档的集合......我已经通过了选项allowDiskUse,现在它抛出错误
TypeError: callback.apply is not a function
我已经搜索过这个,但可以得到解决方案...请帮忙
const pictures = await Picture.aggregate([
{ $sort: { createdAt: -1 }},
{ $group: {
_id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } },
pictures: {
$push: {
_id: '$_id',
image: '$image',
time: { $dateToString: { format: "%H:%M:%S", date: "$createdAt" } }
}
}
}},
{ $limit: 50 },
{ $project: {
_id: false,
createdAt: '$_id',
pictures: '$pictures'
}}
], { allowDiskUse: true })
Run Code Online (Sandbox Code Playgroud)
Mongodb 版本 - 3.6 …
mongoose mongodb node.js mongodb-query aggregation-framework
我有以下查询,首先对文档进行排序,然后跳过并限制 10 条记录,以下是我的查询:
db.getCollection('jobpostings').aggregate([
{"$match":{
"expireDate":{"$gte": ISODate("2018-08-12T00:00:00.000Z")},
"publishDate":{"$lt": ISODate("2018-08-13T00:00:00.000Z")},
"isPublished":true,
"isDrafted":false,
"deletedAt":{"$eq":null},
"deleted":false,
"blocked":{"$exists":false}
}},
{"$lookup":{"from":"companies","localField":"company.id","foreignField":"_id","as":"companyDetails"}},
{"$match":{"companyDetails":{"$ne":[]}}},
{"$sort":{
"isFeatured":-1,
"refreshes.refreshAt":-1,
"publishDate":-1
}},
{"$skip":0},
{"$limit":10},
{"$project":{
"position":1,"summary":1,"company":1,"publishDate":1,
"expireDate":{"$dateToString":{"format":"%Y-%m-%d","date":"$expireDate"}},
"locations":1,"minimumEducation":1,"workType":1,"skills":1,"contractType":1,
"isExtensible":1,"salary":1,"gender":1,"yearsOfExperience":1,"canApplyOnline":1,"number":1,
"isFeatured":1,"viewsCount":1,
"status":{"$cond":{
"if":{"$and":[
{"$lt":["$publishDate", ISODate("2018-08-13T00:00:00.000Z")]},
{"$gt":["$publishDate", ISODate("2018-08-11T00:00:00.000Z")]}]},"then":"New",
"else":{"$cond":{
"if":{"$lt":["$publishDate",ISODate("2018-08-12T00:00:00.000Z")]},"then":"Old","else":"Future"}}}},
"companyDetails.profilePic":1,"companyDetails.businessUnits":1,"companyDetails.totalRatingAverage":1,
"expiringDuration":{"$floor":{"$divide":[{"$subtract":["$expireDate",ISODate("2018-08-12T00:00:00.000Z")]},
86400000]}},
"companyDetails.totalReviews":{"$size":{"$ifNull":[{"$let":{"vars":{
"companyDetailOne":{"$arrayElemAt":["$companyDetails",0]}},"in":"$$companyDetailOne.reviews"}},[]]}}}}
Run Code Online (Sandbox Code Playgroud)
])
现在将上述结果与以下结果进行比较skip=10, limit=10:突出显示的文档在第二页中是重复的(skip=10, limit=10):

对于其他文档,其他页面中也存在相同的内容。