mongodb-query

Group and count using aggregation framework

寵の児 提交于 2021-02-05 07:48:07
问题 I'm trying to group and count the following structure: [{ "_id" : ObjectId("5479c4793815a1f417f537a0"), "status" : "canceled", "date" : ISODate("2014-11-29T00:00:00.000Z"), "offset" : 30, "devices" : [ { "name" : "Mouse", "cost" : 150, }, { "name" : "Keyboard", "cost" : 200, } ], }, { "_id" : ObjectId("5479c4793815a1f417d557a0"), "status" : "done", "date" : ISODate("2014-10-20T00:00:00.000Z"), "offset" : 30, "devices" : [ { "name" : "LCD", "cost" : 150, }, { "name" : "Keyboard", "cost" : 200,

Group records by month and count them - Mongoose, nodeJs, mongoDb

二次信任 提交于 2021-02-02 10:00:25
问题 I need to query in database (mongoose) and get back the number of sales made each month of the year for one particular product (within one year period). I am new to node and mongoDb and I have come with a 'dummy' solution where I query in database and get back all the results for one product and than I use 3 loops to split the results in months but I think that it uses more resources than it should and it will use even more if it gets filled with more data, so I need help making a database

Group records by month and count them - Mongoose, nodeJs, mongoDb

扶醉桌前 提交于 2021-02-02 09:57:44
问题 I need to query in database (mongoose) and get back the number of sales made each month of the year for one particular product (within one year period). I am new to node and mongoDb and I have come with a 'dummy' solution where I query in database and get back all the results for one product and than I use 3 loops to split the results in months but I think that it uses more resources than it should and it will use even more if it gets filled with more data, so I need help making a database

MongoDB get all values of 1 field

ε祈祈猫儿з 提交于 2021-01-29 17:18:48
问题 So I know that in mongodb, I can get all values and then iterate through them but I was wondering if in mongodb there is an option to get only a certain field. So for example suppose my collection is structured as follows: {_id: { field1,field2,field3} } {description: { field1,field2,field3, data:{ field1,field2,field3 } } } {otherdata: {field1,field2,field3} } Now, I need the value of description.data.field1 so any suggestions on how to do that? So far I have tried using following commands:

MongoDB Atlas initial high number of connections and storage

心已入冬 提交于 2021-01-29 10:02:23
问题 I just created a new empty cluster on mongoDB atlas. I chose the M20 cluster tier with 60GB storage limit. However, as soon as my cluster was created, it showed that there are 28 current connections on the server hosting the replica set (keeps increasing and decreasing, even reached 35). It also shows that the current disk space used is 6.3GB even though the cluster was just created and empty. Is this normal on Atlas? And why is the number this high? This bugs me as there is a limit on the

MongoDB: how to update only one element in the document?

允我心安 提交于 2021-01-29 09:17:48
问题 I want to updates only one element in the document to use put request I tried PUT localhost:3000/api/memo/5b8e382d61984255d879f872 { text: "updated!" { but, it updated like this { text: "updated!", imgUrl: null, tag: null, ... } Is there a way to updates one element in document? await Memo.where('_id') .equals(req.params.id) .updateOne({ imgUrl: req.body.imgUrl, text: req.body.text, tag: req.body.tag, user: req.body.user, loc: req.body.loc }) 回答1: You can use the following solution to update

MongoDB: how to update only one element in the document?

谁说胖子不能爱 提交于 2021-01-29 09:14:23
问题 I want to updates only one element in the document to use put request I tried PUT localhost:3000/api/memo/5b8e382d61984255d879f872 { text: "updated!" { but, it updated like this { text: "updated!", imgUrl: null, tag: null, ... } Is there a way to updates one element in document? await Memo.where('_id') .equals(req.params.id) .updateOne({ imgUrl: req.body.imgUrl, text: req.body.text, tag: req.body.tag, user: req.body.user, loc: req.body.loc }) 回答1: You can use the following solution to update

merge multiple documents into one document with both document fields in MongoDB

此生再无相见时 提交于 2021-01-29 08:48:37
问题 I have multiple documents In MobgoDB How to do to the group on "abc" and "xyz" and get one document. Please see the "Output Document". need to do the union with ( Document 1 U Document 2 ) and (Document 1 U Document 3) . U= Union Document 1 { "data": { "Inside_data": { "project": { "abc": { "alpha": 4, "beta" : 45 }, "xyz": { "alpha": 214, "beta" : 431 } } } } } Document 2 "Deal": { "name": "abc", "url" : "www.abc.com, "email": [ "abc@gmail.com"], "total": 2 } Document 3 "Deal": { "name":

Mysteriously Invalid Mongoose Query

╄→尐↘猪︶ㄣ 提交于 2021-01-29 07:35:46
问题 I'm getting stuck on a stubborn bug in my MEPN app. This pseudo-code is supposed to assemble a Mongoose query from the options submitted in a user form, and then search a collection using that query. var query = []; query.push("{name:"+req.body.name+"}"); query.push("{status:{$in:["+req.body.statusarray+"]}}"); query.push("{range:{$gte:"+req.body.min+",$lte:"+req.body.max+"}}"); Collection.find(query, function(error, cursor){ if(error) console.log("ERROR: "+error); else //do something })

MongoDB query with $exists and $elemMatch doesn't work

旧巷老猫 提交于 2021-01-29 06:11:32
问题 E.g. I have Java objects: public class Foo { private Example example; } public class Example { private String str1; private String str2; } Field example can be null. I need to get all Foo objects where str1 contains e.g. "text". According to documentation I tried: @Query(value = "{ 'example' : { $exists : true, $elemMatch : { str1 : { $regex: '.*?0.*'} } } }") but it returns empty Page. 回答1: Define the query in the repository: @Repository public interface FooRepo extends MongoRepository<Foo,