mongodb-query

How to project updated values only using findOneAndUpdate in embedded array Mongoose?

回眸只為那壹抹淺笑 提交于 2019-12-13 03:49:28
问题 Currently my User model looks like: { _id: 'SomeId' firstName: 'John', lastName: 'Cena', books: [ { _id: 'xyz', title: 'a', author:'b', ratings:[ {source:'source1', value:"8"}, {source:'source2', value:"9"}] }, { _id: 'abc', title: 'c', author:'d', ratings:[ {source:'source3', value:"7"}, {source:'source4', value:"5"}] } ] } After making an findOneAndUpdate query to update rating=>value of 1st book object(_id: "xyz") from 8 to 10 for a given source(say "source1") : let up={ 'books.$[book]

Performance tuning MongoDB query/update?

天大地大妈咪最大 提交于 2019-12-13 03:43:32
问题 So I have a MongoDB instance where I am trying to update data in one collection with data from another collection. The two collections are participants with about 180k documents and questions with about 95k documents. Documents in participants typically look something like this: { "_id" : ObjectId("52f90b8bbab16dd8594b82b4"), "answers" : [ { "_id" : ObjectId("52f90b8bbab16dd8594b82b9"), "question_id" : 2081, "sub_id" : null, "values" : [ "Yes" ] }, { "_id" : ObjectId("52f90b8bbab16dd8594b82b8

$lookup and push new data

我们两清 提交于 2019-12-13 03:42:07
问题 Hi i'm having a discussion object collection and a user deatils collection. Discussion collection stores the participants username in an array of strings. Discussion collection is as follows: [{ "_id": "5a4dbdaab46b426863e7ead3", "topic": "test", "topicDesc": "test123", "createdOn": "2018-01-04T05:37:46.088Z", "participants": ["akhil","ben"] //[usernames] }] The user details collection is as follows: [{ "_id": "59e6d6ba02e11e1814481022", "username": "ben", "name": "Ben S", "email": "qwerty

Aggregation is very slow

时间秒杀一切 提交于 2019-12-13 03:34:07
问题 I have a collection with a structure similar to this. { "_id" : ObjectId("59d7cd63dc2c91e740afcdb"), "dateJoined": ISODate("2014-12-28T16:37:17.984Z"), "activatedMonth": 5, "enrollments" : [ { "month":-10, "enrolled":'00'}, { "month":-9, "enrolled":'00'}, { "month":-8, "enrolled":'01'}, //other months { "month":8, "enrolled":'11'}, { "month":9, "enrolled":'11'}, { "month":10, "enrolled":'00'} ] } month in enrollments sub document is a relative month from dateJoined. activatedMonth is a month

Mongodb query - Does sequence of key matters in usage of compound index

亡梦爱人 提交于 2019-12-13 02:57:26
问题 Suppse I created index like following :- db.collection.createIndex( { propA: 1, propB: 1, propC:1 } ) and I query like following :- db.collection.find({propB:'x', propC: 'y', propA:'z'}) will mongo query engine use the index created above or not. Does sequence of key matters in usage of compound index while writing query? 回答1: The order of keys in a query doesn't matter: MongoDB is smart enough to look at all the queried properties and find a suitable index. However, the order of keys defined

How do I use the mongoDB Comparison Query Operator to check for values between createdAt dates?

时光怂恿深爱的人放手 提交于 2019-12-13 02:49:37
问题 I would like to view all the created users Meteor.users.find().fetch() that were created in the database between the beginning of 1st September and the end of 30th September. How do I achieve this in code. Find inline my failed attempt: var beginningOfMonth = new Date(); beginningOfMonth.setMonth(8, 1); console.log("beginning Of the Month: "+beginningOfMonth); The code above yeilds: beginning Of the Month: Sat Sep 01 2018 21:44:00 GMT+0300 (East Africa Time) ... var endOfMonth = new Date();

Updating a double nested embedded document mongo

邮差的信 提交于 2019-12-13 02:26:23
问题 I've got a collection of questions. Each question has an array of answers. Each answer has an array of votes in the form of user id's. Here is an sample question: { "_id" : "PeD7YxzPdbm9MM6rQ", "answers" : [ { "_id" : "481f791b003ffc58f2adf200", "answer" : "d1", "votes" : [] }, { "_id" : "cdf02143fa7a10e939df636b", "answer" : "d2", "votes" : [ "USER1" ] }, { "_id" : "4d552e31fd45a9b2b25e2e3a", "answer" : "d3", "votes" : [ "USER2" ] } ] } I've replaced the IDs "RdqMhRkboWzfxNt8a" with USER1

MongoDB elemMatch does not work expected

余生颓废 提交于 2019-12-13 02:22:43
问题 I am trying to get subdocuments which are matched my conditions. My MongoDB query like that: db.SCSIssue.find( { _id: ObjectId("5439a2992ea8cc0f70feef2d") }, { Statuses: { $elemMatch: { StatusID: { $gte : NumberLong(521055087020736513) } } } } ) My schema like that: { .... "_id": ObjectId("5439a2992ea8cc0f70feef2d"), .... "Statuses": [{ .... "StatusID": NumberLong(525623822633172993), },{ .... "StatusID": NumberLong(521055087020736513), },{ .... "StatusID": NumberLong(521060802959532033), }]

How to projection/filter embedded keys with applying regex on key

*爱你&永不变心* 提交于 2019-12-13 02:20:15
问题 I wonder know how to return the specified embedded keys ? by the similar query db.collection.find({"table_name": "change_in_real_gdp", {"data.*.actual":1, "data.*. upper_end_of_range"}}) Original document { "_id": "2012-06-20_change_in_real_gdp", "table_name": "change_in_real_gdp", "data": { "2007": { "actual": "4.8", "upper_end_of_range": "-", "upper_end_of_central_tendency": "-", "lower_end_of_central_tendency": "-", "lower_end_of_range": "-" }, "2008": { "actual": "6.9", "upper_end_of

How can I execute a statement AFTER a loop finishes in javascript?

假如想象 提交于 2019-12-13 02:12:49
问题 I'm querying a mongo database to retrieve the tiles for the display in rougelike game. This is the function I use: function get_display(){ var collections = ['austinsroom']; var db = mongojs(uri, collections); var temphtml = ''; for(var j = 0; j < 3; j++) { console.log("y=" + String(j)); db.austinsroom.find({"y": j}, {}).sort({"x": 1}, function(err, records) { if(err) { console.log("There was an error executing the database query."); return; } var i = records.length; while(i--) { temphtml +=