mongodb-query

Use Aggregate with $group in mongodb

…衆ロ難τιáo~ 提交于 2019-12-11 08:55:56
问题 I have data in worksheets collection like below: /* 1 */ { "_id" : ObjectId("5c21d780f82aa31334ab6506"), "isBilling" : true, "hours" : 6, "userId" : ObjectId("5c1f38a1d7537d1444738493"), } /* 2 */ { "_id" : ObjectId("5c21d780f82aa31334ab6507"), "isBilling" : true, "hours" : 4, "userId" : ObjectId("5c1f38a1d7537d1444738493"), } /* 3 */ { "_id" : ObjectId("5c21e10fae07cc1204a5b647"), "isBilling" : false, "hours" : 8, "userId" : ObjectId("5c1f388fd7537d1444738492"), } I have to create a

Mongodb find query

孤街浪徒 提交于 2019-12-11 08:47:19
问题 seasons.json { "_id" : "unique_1", "spring" : [{ "fruit" : "mango", "person_id" : [101.0, 102.0, 103.0, 104.0] }, { "fruit" : "banana", "person_id" : [151.0, 152.0, 153.0, 154.0] }], "summer" : [{ "fruit" : "mango", "person_id" : [201.0, 202.0, 203.0, 204.0] }, { "fruit" : "banana", "person_id" : [251.0, 252.0, 253.0, 254.0] }], "fall" : [{ "fruit" : "mango", "person_id" : [301.0, 302.0, 303.0, 304.0] }, { "fruit" : "banana", "person_id" : [351.0, 352.0, 353.0, 354.0] }], "winter" : [{ "fruit

Mongodb update $pull always return 1

冷暖自知 提交于 2019-12-11 08:45:48
问题 I want to delete an embedded object from an array, using this query. It works fine, but the result is always 1. No matter if an array element was pulled or not. This makes it hard to respond to the request in a proper way. db.clients.update({_id: someId}, {$pull: {someArray: {id: req.params.id}}}, function(err, result) { if (err) return next(err); res.json(result); }); Q) Is there a way to make this query return for example null or zero when an element is not pulled? 回答1: While there have

MongoDB Mongoose aggregate query deeply nested array remove empty results and populate references

帅比萌擦擦* 提交于 2019-12-11 08:01:48
问题 This question is a follow up to a previous question for which I have accepted an answer already. I have an aggregate query that returns the results of a deeply nested array of subdocuments based on a date range. The query returns the correct results within the specified date range, however it also returns an empty array for the results that do not match the query. Technologies: MongoDB 3.6, Mongoose 5.5, NodeJS 12 Question 1: Is there any way to remove the results that don't match the query?

Comparing two fields of mongo collection using c# driver in mono

一笑奈何 提交于 2019-12-11 07:59:58
问题 Am completely new to Mongodb and C# driver. Development is being done using Monodevelop on Ubuntu 14.04 and Mongodb's version is 3.2.10 : Currently my code has a POCO as below: public class User { public String Name { get; set;} public DateTime LastModifiedAt { get; set;} public DateTime LastSyncedAt { get; set;} public User () { } } Have been able to create a collection and also to add users. How do I find users, whose LastModifiedAt timestamp is greater than LastSyncedAt timestamp ? Did

How to return only the first occurrence of an ID with Mongoose?

跟風遠走 提交于 2019-12-11 07:39:31
问题 I have a large collection called Messages , like so: { user: 94fg844f, event: null, group: null, name: "Jake", text: "Hello world" }, { user: fje93jrg4, event: null, group: null, name: "Bob" text: "Testing" }, { user: fje93jrg4, event: null, group: null, name: "Bob" text: "Text here" }, { user: null, event: d0j3n9fn3, group: null, name: "My Event" text: "Testing 2" }, { user: null, event: d0j3n9fn3, group: null, name: "My Event" text: "Another text" } I need to get the first occurrence of the

Mongodb group and project operators

落花浮王杯 提交于 2019-12-11 07:26:37
问题 I'm trying to group by "group_name" and select fields "group_name" and "group_id" with php driver using aggregation framework: Array ( [$project] => Array ( [group_name] => 1 [group_id] => 1 ) [$group] => Array ( [_id] => $group_name [total_sum] => Array ( [$sum] => 1 ) ) ) I'm getting this: [errmsg] => exception: A pipeline stage specification object must contain exactly one field. However, when I'm using only $project or $group operator it works just fine. 回答1: The issue is your grouping

Count where value does not match array

筅森魡賤 提交于 2019-12-11 07:24:08
问题 I have following offers collection Here readBy contains the _id of the users... Now I want to count the number of unRead offers for the userId = "5add82620d7f5b38240c63d4" { "_id" : ObjectId("5aeaab5ed6a9c97d0209260a"), "expiresIn" : ISODate("2018-05-30T18:30:00.000Z"), "name" : "Trip ", "readBy" : [ ObjectId("5add82620d7f5b38240c63d4"), ObjectId("5add82620d7f5b38240c63c6") ], "__v" : 0 } { "_id" : ObjectId("5aeaab7dd6a9c97d0209260b"), "expiresIn" : ISODate("2018-05-29T18:30:00.000Z"), "name"

How to build query for relating parent document's field to embedded array document's field. Using MongoDB aggregation Operators

扶醉桌前 提交于 2019-12-11 06:59:27
问题 Consider the following document in the collection named 'CityAssociation' { "_id" : "MY_ID", "ThisCityID" : "001", "CityIDs" : [{ "CityID" : "001", "CityName" : "Bangalore" }, { "CityID" : "002", "CityName" : "Mysore" }], "CityUserDetails": { "User" : "ABCD" } } Now I have User value i.e. in above case I have value ABCD and want to find it with only city where the first level's field ThisCityID matches to the embedded array documnet's field CityID . Finally I need to project as follows (for

Return original documents only from mongoose group/aggregation operation

六眼飞鱼酱① 提交于 2019-12-11 06:57:14
问题 I have a filter + group operation on a bunch of documents (books). The grouping is to return only latest versions of books that share the same book_id (name). The below code works, but it's untidy since it returns redundant information: return Book.aggregate([ { $match: generateMLabQuery(rawQuery) }, { $sort: { "published_date": -1 } }, { $group: { _id: "$book_id", books: { $first: "$$ROOT" } } } ]) I end up with an array of objects that looks like this: [{ _id: "aedrtgt6854earg864", books: {