aggregation-framework

Mongodb group by dbref field

霸气de小男生 提交于 2019-12-24 04:27:32
问题 I need group products by model. Each product has model field - DBRef to Models collection. I tried use this aggregate query, but have error FieldPath field names may not start with '$'. Aggregation query: db.Products.aggregate([ { $project: { _id: 0, model: 1, isActive: 1 } }, { $group: { _id: "$model.$id", actives: { $push: "$isActive" } }} ]); Example of product document: { _id: ObjectId("54f48610e31701d2184dede5"), isActive: true, model: { $db: "database", $ref: "Models", $id: ObjectId("..

Use $mergeObjects inside ReplaceRoot pipeline stage in Spring MongoDB

早过忘川 提交于 2019-12-24 03:44:22
问题 i'm looking to reproduce this snipet into Java code : db.getCollection('admins_comptes_client_ceov4').aggregate([ {$lookup: {from: "contrats_ceov4",localField: "CUSTOMERNUMBER",foreignField: "CUSTOMERNUMBER",as: "arrayForeignObject"} {$unwind: { path: "$arrayForeignObject", preserveNullAndEmptyArrays: true}}, {$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }}, { $project: { "arrayForeignObject": 0, "_id": 0 } }, { $out: "aggregate" }]) I'm here so far :

Is there a workaround to allow using a regex in the Mongodb aggregation pipeline

纵饮孤独 提交于 2019-12-24 03:43:32
问题 I'm trying to create a pipeline which will count how many documents match some conditions. I can't see any way to use a regular expression in the conditions though. Here's a simplified version of my pipeline with annotations: db.Collection.aggregate([ // Pipeline before the issue {'$group': { '_id': { 'field': '$my_field', // Included for completeness }, 'first_count': {'$sum': { // We're going to count the number '$cond': [ // of documents that have 'foo' in {'$eq: ['$field_foo', 'foo']}, 1,

How to sort, select and query subdocument in mongoose

▼魔方 西西 提交于 2019-12-24 01:43:08
问题 So I'm trying to sort subdocument, but also select and everything. It seems I can't with a regular query so I tried w/ aggregate mongoose = require("mongoose"); mongoose.connect("localhost:27017", function(err) { mongoose.connection.db.dropDatabase(); Story = mongoose.model("Story", { title: String, comments: [{ author: String, content: String }] }); sample = new Story({ title: "Foobar", comments: [ { author: "A author", content: "1 Content" }, { author: "B author", content: "2 Content" } ] }

Sort By length of array mongodb query

让人想犯罪 __ 提交于 2019-12-24 01:36:10
问题 I have MongoDB document like below: is there a query to Sort the documents with the length of subscribedGroups? which can be ascending or descending. { "_id" : ObjectId("58c29d9ec79d585c0e16b110"), "subscribedGroups" : [ ObjectId("5b28a9190c8c0d0014b03a0c"), ObjectId("5b2930650b813a0014a3294d"), ObjectId("5b29f1b5243d470014d6d351") ] } 回答1: You can find the size of the subscribedGroups using $size and then easily $sort with the length of the array db.collection.aggregate([ { $addFields: {

Why even after using async await for multiple calls still got empty response?

ε祈祈猫儿з 提交于 2019-12-24 01:15:36
问题 Here is simple node route in which calling an asynchronous api . What needed is to return data after the looping. But It is returning blank object. try { const array = ["brunch", "lunch", "crunch"] const data = {} array.map(async(d) => { const venue = await Venue.find({ "category": { "$in": [d] }}) data[d] = venue }) return data } catch(err) { throw err } Please help me to achieve this 回答1: There is a better way to get the desired result with MongoDB and no need to loop, use the aggregation

Need a distinct count on multiple fields that were joined from another collection using mongodb aggregation query

风流意气都作罢 提交于 2019-12-24 00:48:45
问题 I'm trying to use a mongodb aggregation query to join($lookup) two collections and then distinct count all the unique values in the joined array. So my two collections look like this: events- { "_id" : "1", "name" : "event1", "objectsIds" : [ "1", "2", "3" ], } Objects { "_id" : "1", "name" : "object1", "metaDataMap" : { "SOURCE" : ["ABC", "DEF"], "DESTINATION" : ["XYZ", "PDQ"], "TYPE" : [] } }, { "_id" : "2", "name" : "object2", "metaDataMap" : { "SOURCE" : ["RST", "LNE"], "TYPE" : ["text"]

The Foreign field of $lookup could be the field of nested document?

*爱你&永不变心* 提交于 2019-12-24 00:38:00
问题 $lookup is used to perform a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing in Mongo. { $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> } } Could the foreignField be the field of the nested document of from collection? For example, there are two collections as following. history

How to $setDifference in array & Object using Mongo DB

别来无恙 提交于 2019-12-23 23:12:32
问题 UserDetails { "_id" : "5c23536f807caa1bec00e79b", "UID" : "1", "name" : "A", }, { "_id" : "5c23536f807caa1bec00e78b", "UID" : "2", "name" : "B", }, { "_id" : "5c23536f807caa1bec00e90", "UID" : "3", "name" : "C" } UserProducts { "_id" : "5c23536f807caa1bec00e79c", "UPID" : "100", "UID" : "1", "status" : "A" }, { "_id" : "5c23536f807caa1bec00e79c", "UPID" : "200", "UID" : "2", "status" : "A" }, { "_id" : "5c23536f807caa1bec00e52c", "UPID" : "300", "UID" : "3", "status" : "A" } Groups { "_id" :

Aggregating By Date in Mongodb

不羁的心 提交于 2019-12-23 22:52:38
问题 I am writing a piece of functionality in which I am required to group by Date. Here is how I do currently: //Assuming this is my sample document in the collection { "_id" : ObjectId("56053d816518fd1b48e062f7"), "memberid" : "7992bc31-c3c5-49e5-bc40-0a5ba41af0bd", "sourceid" : NumberInt(3888), "ispremium" : false, "createddate" : { "DateTime" : ISODate("2015-09-25T12:26:41.157+0000"), "Ticks" : NumberLong(635787808011571008) }, "details": { //a large sub-document } } Given the member id, start