aggregation-framework

MongoDB divide returns null when dividing by other variable

最后都变了- 提交于 2020-01-30 13:13:09
问题 I am trying to compute some numbers, but MongoDB returns null when using previous computed variable in $divide operator This is the query: db.apps.aggregate( {$project : { a: {$multiply : [2,2]}, b: {$divide: [5, "$a"]}, c: {$divide: [5, 4]} }}) Why "b" is null in the result: /* 0 */ { "result" : [ { "_id" : ObjectId("5361173d93861f6e5239714e"), "a" : 4, "b" : null, "c" : 1.25 }, { "_id" : ObjectId("536192c1938652d039fa0051"), "a" : 4, "b" : null, "c" : 1.25 } ], "ok" : 1 } EDIT: Thanks to

$replaceRoot in mongodb aggregation

我是研究僧i 提交于 2020-01-30 08:10:13
问题 I have a collection like this: { "_id" : ObjectId("5bd1686ba64b9206349284db"), "type" : "Package", "codeInstances" : [ { "name" : "a", "description" : "a" }, { "name" : "b", "description" : "b1" }, { "name" : "b", "description" : "b2" } ] } { "_id" : ObjectId("5bd16ab8a64b92068d485054"), "type" : "Package", "codeInstances" : [ { "name" : "a", "description" : "a" }, { "name" : "b", "description" : "b3" } ] } The following structure is what I want: { "name" : "b", "description" : "b1" }, {

Add field of array element in MongoDB aggregation

瘦欲@ 提交于 2020-01-30 05:46:10
问题 I have a collection of documents containing an array of objects: db.collection.insert({ arr: [ { id: 1, text: 'foo' }, { id: 2, text: 'bar' }, ] }); Is there a way to extract/project/add a field of one element in that array? For example, the text field of the first element of the array. I've tried various variations of $addFields in MongoPlayground, db.collection.aggregate([ { $addFields: { text1: '$arr.text' } } ]); but nothing produced just one text field. At best, I got both, with the

$nin with the $expr

微笑、不失礼 提交于 2020-01-30 05:16:15
问题 I have a query to find if a user CreatedBy is in a SharedWith . I want to inverse the query to check if the CreatedBy is not in SharedWith . [ { "$match": { "$and": [ { "$and": [ { "SharedWith": { "$exists": true } }, { "$expr": { "$in": [ "$CreatedBy", "$Multi_User" ] } } ] } ] } } ] MongoDB does not support a direct $nin or $not for $and query. Any idea how to achieve this. The user document looks like this, Collection = [ {"CreatedBy": {"_id": "User001", "Email": "user@eg.com", "Name":

Nested conditional MongoDB query

纵然是瞬间 提交于 2020-01-25 20:24:22
问题 Im having a hard time trying to run some nested queries with a conditional statement of an item inside an array. this is how my documents looks like. I would like to get a summary such as sum and average and alarmedCount (count every time Channels.AlarmStatus == "alarmed") of each "Channel" based on Channels.Id. I got sum and average to work but cant get the right query for alarmedCount { "_id" : "55df8e4cd8afa4ccer1915ee" "location" : "1", "Channels" : [{ "_id" : "55df8e4cdsafa4cc0d1915r1",

MongoDb Exists per column

痞子三分冷 提交于 2020-01-25 07:28:27
问题 Running an aggregation such as the following: [ { "$match":{ "datasourceName":"Startup Failures", "sheetName":"Data", "Cost":{ "$exists":true }, "Status":{ "$exists":true } } }, { "$group":{ "Count of Cost":{ "$sum":1 }, "Count of Status":{ "$sum":1 }, "_id":null } }, { "$project":{ "Count of Cost":1, "Count of Status":1 } } ] The result of the exists filters actually filters out the whole documents where "Cost" or "Status" do not exist. Such that the projection (Count) of both Cost and

Present in input array

ⅰ亾dé卋堺 提交于 2020-01-25 07:05:31
问题 We have a stocks collection: stocks: {"_id" : ObjectId("xxx"),"scrip" : "xxxxx2" } {"_id" : ObjectId("xxy"),"scrip" : "xxxxx3" } {"_id" : ObjectId("xyy"),"scrip" : "..." } Given an input array of scrips [xxxxx7,xxxxx2,xxxxx3,xxxxx8] ,we need to return an array of the scrips not present in the stocks collection. So the expected output is : [xxxxx7,xxxxx8] Is there a way to achieve this using Filter.expr and $setIsSubset(or any other alternative). Not able to get an example of the same. Help is

How to Make a Lookup connection between two Collection

限于喜欢 提交于 2020-01-25 00:21:05
问题 Goal: This sql and its result should be the same result from mongoDB's query code. In order words, same result but for mongoDB. Problem: How to you make a lookup connection in relation to People and Role in Mongo DB's query code? Info: I'm new in mongo DB SQL code SELECT a.*, '.' AS '.', b.*, '.' AS '.', c.* FROM [db1].[dbo].[People_Course_Grade] a INNER JOIN [db1].[dbo].[People] b on a.PeopleId = b.PeopleId INNER JOIN [db1].[dbo].[Role] c on b.RoleId = c.RoleId Json data: Role: [{"RoleId":1,

Store location data in Mongodb document

南楼画角 提交于 2020-01-24 22:58:26
问题 In my current project , I am storing the location data in the following format in a Mongodb document "location" : { "loc" : { "lng" : -118.15592692, "lat" : 34.03566804 }, "geocode" : { "city" : "East Los Angeles", "state" : "CA", "zipcode" : "90022", "countrycode" : "US", "country" : "United States" } } There is a 2d index created on location.loc field:- { "location.loc" : "2d" } But the geospatial queries are taking long time when the result set is large i.e more than 7000 records. it seems

Store location data in Mongodb document

◇◆丶佛笑我妖孽 提交于 2020-01-24 22:58:06
问题 In my current project , I am storing the location data in the following format in a Mongodb document "location" : { "loc" : { "lng" : -118.15592692, "lat" : 34.03566804 }, "geocode" : { "city" : "East Los Angeles", "state" : "CA", "zipcode" : "90022", "countrycode" : "US", "country" : "United States" } } There is a 2d index created on location.loc field:- { "location.loc" : "2d" } But the geospatial queries are taking long time when the result set is large i.e more than 7000 records. it seems