aggregation-framework

Graphql returns null id for mongoose aggregation query

浪尽此生 提交于 2020-01-15 11:42:51
问题 Graphql returns null id for mongoose aggregation query, but works ok other mongoose queries. Here is my mongoose schema: const { Schema } = mongoose; const ObjectId = Schema.Types.ObjectId; const productSchema = new Schema({ _id: ObjectId, price: Number }) const Product = mongoose.model('Product', productSchema, 'Product') Here is my Graphql schema: type Product { id: ID price: String } Graphql normal query: context.Product.findOne() Result with console.log: [ { price: 10, _id:

Use MongoDB aggregation to find set intersection of two sets within the same document

纵然是瞬间 提交于 2020-01-15 09:07:45
问题 I'm trying to use the Mongo aggregation framework to find where there are records that have different unique sets within the same document. An example will best explain this: Here is a document that is not my real data, but conceptually the same: db.house.insert( { houseId : 123, rooms: [{ name : 'bedroom', owns : [ {name : 'bed'}, {name : 'cabinet'} ]}, { name : 'kitchen', owns : [ {name : 'sink'}, {name : 'cabinet'} ]}], uses : [{name : 'sink'}, {name : 'cabinet'}, {name : 'bed'}, {name :

MongoDB Aggregation Pipeline C#

自闭症网瘾萝莉.ら 提交于 2020-01-15 08:28:09
问题 I have the following Mongo query: db.BusRatings.aggregate( [ { $match: { VId: 2020}}, { $project: { vid: '$VId', sb:'$SvcRating.StaffBehavior' ,bq: '$SvcRating.BusQuality' , src: '$SvcDetails.SrcNm', dst: '$SvcDetails.DestNm', py: '$SvcRating.Punctuality'}}, { $group: { _id: {vid: '$vid', src: '$src', dst: '$dst'}, numratings: {$sum: 1}, avgpy : { $avg : '$py'}, avgbq: { $avg: '$bq'},avgsb: { $avg: '$sb'}} }, { $match : { numratings: { $gt: 3 } } }, { $project: {_id: 0, 'vid' : '$_id.vid',

$lookup on ObjectId's in an array

烈酒焚心 提交于 2020-01-15 07:14:08
问题 What's the syntax for doing a $lookup on a field that is an array of ObjectIds rather than just a single ObjectId? Example Order Document: { _id: ObjectId("..."), products: [ ObjectId("..<Car ObjectId>.."), ObjectId("..<Bike ObjectId>..") ] } Not Working Query: db.orders.aggregate([ { $lookup: { from: "products", localField: "products", foreignField: "_id", as: "productObjects" } } ]) Desired Result { _id: ObjectId("..."), products: [ ObjectId("..<Car ObjectId>.."), ObjectId("..<Bike ObjectId

How Do I return a boolean in a mongodb category

柔情痞子 提交于 2020-01-15 06:05:49
问题 If I have a collection Friends like: {'_id': ObjectId('abcdef1'), 'user': 'Jim', 'user2': 'Jon'} {'_id': ObjectId('abcdef2'), 'user': 'Jim', 'user2': 'Fred'} {'_id': ObjectId('abcdef3'), 'user': 'Jon', 'user2': 'Jim'} and I want a query that gets all people that are friends with me, and a boolean for whether or not I'm friends with them. I was hoping for some find statement that would be like: Find all people I'm friends with and add a field that says whether they're friends with me My

How Do I return a boolean in a mongodb category

梦想的初衷 提交于 2020-01-15 06:05:11
问题 If I have a collection Friends like: {'_id': ObjectId('abcdef1'), 'user': 'Jim', 'user2': 'Jon'} {'_id': ObjectId('abcdef2'), 'user': 'Jim', 'user2': 'Fred'} {'_id': ObjectId('abcdef3'), 'user': 'Jon', 'user2': 'Jim'} and I want a query that gets all people that are friends with me, and a boolean for whether or not I'm friends with them. I was hoping for some find statement that would be like: Find all people I'm friends with and add a field that says whether they're friends with me My

Use createView with allowDiskUse

蹲街弑〆低调 提交于 2020-01-14 10:20:06
问题 In the mongo aggregation framework is possible to use the option {allowDiskUse:true} . This is really useful when some heavy operations such as sorting, which cannot be performed in memory. I'm trying to do the same with createView (available in Mongo 3.4), but I cannot find the place where allowDiskUse can be introduced. In the normal aggregation framework: db.mydb.aggregate([....,{$sort:{"a":-1}}],{allowDiskUse:true}) works, but: db.createView("newview","mydb",[....,{$sort:{"a":-1}}],

How to match by 'undefined' value in MongoDB Aggregation Framework?

本小妞迷上赌 提交于 2020-01-14 07:00:08
问题 How can I search for the records filtering in a field that has an undefined value: db.records.aggregate({ $match: { myField: "undefined", } }) 回答1: If you want to filter out documents that have some fields missing, use the $exists operator. This works on my machine : > db.test.drop() true > db.test.insert( {'Hello':'World!', 'myField':42}) > db.test.insert( {'Hello again':'World!'}) > db.test.aggregate({'$match':{ 'myField':{'$exists':false} }}) { "result" : [ { "_id" : ObjectId(

Reformat collection using aggregation pipeline in Mongodb

若如初见. 提交于 2020-01-14 03:32:05
问题 I have a collection of next objects: { "_did": "213412323234", "metadata": [ { "id": "Language", "value": "EN" }, { "id": "City", "value": "New York" }, { "id": "Gender", "value": "Male" } ] } After using some pipeline operators (maybe $project), I expect something like this: { "id": "213412323234", "metadata": { "Language": "EN", "City": "New York", "Gender": "Male" } } 回答1: Try this query script var results = db.meta.aggregate([{ $project: { id: "$_did", _id: 0, keys: "$metadata.id", values

Mongo DB duplication issue while using sorting with limit and skip in aggregation

放肆的年华 提交于 2020-01-13 04:26:33
问题 Facing an issue of duplicate records while fetching record by sorting with skip and limit: Collection Data: { "_id" : ObjectId("594b507c9b9469ec9da6a78b"), "name" : "F", "percentage" : 60.0, "weightedFilter" : 2.0, "like" : 1.0, "attraction" : 1.0 } { "_id" : ObjectId("594b507c9b9469ec9da6a78c"), "name" : "I", "percentage" : 80.0, "weightedFilter" : 0.0, "like" : 1.0, "attraction" : 1.0 } { "_id" : ObjectId("594b507c9b9469ec9da6a78d"), "name" : "J", "percentage" : 80.0, "weightedFilter" : 1.0