mongodb-query

Update many documents in mongoDB with different values

佐手、 提交于 2020-01-30 04:28:21
问题 I am trying to update two documents in mongoDB, with two different values. I made it with two different callbacks, but is it possible to do it with only one request? My solution: mongo.financeCollection.update( { 'reference': 10 }, { $push: { history: history1 } }, function (err){ if (err){ callback (err); } else { mongo.financeCollection.update( { 'reference': 20 }, { $push: { history: history2 } }, function (err){ if (err){ callback(err); } else { callback(null); } }); } }); Sorry if it is

Update many documents in mongoDB with different values

浪子不回头ぞ 提交于 2020-01-30 04:28:09
问题 I am trying to update two documents in mongoDB, with two different values. I made it with two different callbacks, but is it possible to do it with only one request? My solution: mongo.financeCollection.update( { 'reference': 10 }, { $push: { history: history1 } }, function (err){ if (err){ callback (err); } else { mongo.financeCollection.update( { 'reference': 20 }, { $push: { history: history2 } }, function (err){ if (err){ callback(err); } else { callback(null); } }); } }); Sorry if it is

How to change a field & add a sub-field in Mongodb

房东的猫 提交于 2020-01-26 03:15:38
问题 I have mongo documents like the following: { "_id" : ObjectId("591e36eb2cdce09936d8e511"), "bday" : null, "studentId" : "000004" } I want to make it : { "_id" : ObjectId("591e36eb2cdce09936d8e511"), "bday" : null, "studentId" : { "id" : "000004" } } I have tried the following: db.persons.update({"studentId": "000004"},{$set : {"studentId.id": "000004"}},false,true) But it is giving an error: LEFT_SUBFIELD only supports Object: studentId not: 2 Can anyone help? Thanks, Sumit. 回答1: In your case

$match and update a certain key in object after $unwind in MongoDB

╄→гoц情女王★ 提交于 2020-01-25 09:03:11
问题 I have documents like the following one: { "_id" : ObjectId("5dc027d38da295b969eca67c"), "emp_no" : 10031, "salaries" : [ { "salary" : 40000, "from_date" : "1991-09-01", "to_date" : "1992-08-31" }, { "salary" : 40859, "from_date" : "1992-08-31", "to_date" : "1993-08-31" }, { "salary" : 41881, "from_date" : "1993-08-31", "to_date" : "1994-08-31" } ] } As a goal I need to increment salary by 1000 for every object that has from_date key in a range of 1985-01-01 - 1985-12-31 . So far i came up

How to update subset of a string in MongoDB?

独自空忆成欢 提交于 2020-01-25 08:03:13
问题 I want to update the following subset of a string in monogdb Collection: Paper Field: URL Document Current: Name : House URL : www.home.com/300x300 Document Updated Name : House URL : www.home.com/600x600 I have already tried this but it doesn't seem to be working: db.Paper.find({Name:"House"}).forEach(function(e,i) { e.URL=e.URL.replace("300","600"); db.Paper.save(e); }); Any ideas? 回答1: You can use one of the following aggregations to query and update: db.test.aggregate( [ { $match: { url:

How to update subset of a string in MongoDB?

大憨熊 提交于 2020-01-25 08:03:12
问题 I want to update the following subset of a string in monogdb Collection: Paper Field: URL Document Current: Name : House URL : www.home.com/300x300 Document Updated Name : House URL : www.home.com/600x600 I have already tried this but it doesn't seem to be working: db.Paper.find({Name:"House"}).forEach(function(e,i) { e.URL=e.URL.replace("300","600"); db.Paper.save(e); }); Any ideas? 回答1: You can use one of the following aggregations to query and update: db.test.aggregate( [ { $match: { url:

MongoDB takes different time for with and without hint

梦想的初衷 提交于 2020-01-25 07:06:48
问题 I know, I know, hint forces MongoDB to use a specific index. I have two queries: db.mycol.find({sourceId:ObjectId("596bac5a6f473e1a042bFFFF"),myFlag:false}).count() db.mycol.find({sourceId:ObjectId("596bac5a6f473e1a042bFFFF"),myFlag:false}).hint("sourceId_-1_myFlag_-1").count() Both of these commands take different time: Without hint: 4-6 seconds With hint : 0-1 second However, both of the commands are using the same index: { "sourceId" : -1, "myFlag" : -1 } When I execute db.mycol.aggregate(

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,

How to group records based on array elements using MongoDB

帅比萌擦擦* 提交于 2020-01-25 00:14:24
问题 I have a data collection which contains a set of records in the following format. { "_id" : 22, "title" : "3D User Interfaces with Java 3D", "isbn" : "1884777902", "pageCount" : 520, "publishedDate" : ISODate("2000-08-01T07:00:00Z"), "thumbnailUrl" : "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/barrilleaux.jpg", "longDescription" : "Description", "status" : "PUBLISH", "authors" : [ "Jon Barrilleaux" ], "categories" : [ "Java", "Computer Graphics" ] }, { "_id" : 23, "title"

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