mongodb-update

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:

Can't append to array using string field name [$] when performing update on array fields

和自甴很熟 提交于 2020-01-11 05:04:39
问题 rowsI am attempting to perform a mongodb update on each field in an array of records. An example schema is below: { "_id" : ObjectId("508710f16dc636ec07000022"), "summary" : "", "uid" : "ABCDEF", "username" : "bigcheese", "name" : "Name of this document", "status_id" : 0, "rows" : [ { "score" : 12, "status_id" : 0, "uid" : 1 }, { "score" : 51, "status_id" : 0, "uid" : 2 } ] } So far I have been able to perform single updates like this: db.mycollection.update({"uid":"ABCDEF","rows.uid":1}, {

MongoDB: Update property of subarray just updates the first element

丶灬走出姿态 提交于 2019-12-24 01:08:05
问题 The matching element looks like that: { "_id": { "$oid": "519ebd1cef1fce06f90e3157" }, "from": "Tester2", "to": "Tester", "messages": [ { "username": "Tester2", "message": "heeey", "read": false }, { "username": "Tester", "message": "hi!", "read": false }, { "username": "Tester2", "message": "test", "read": false } ], } Now I try to set the read property to the current date just of the subelements where the username is not equal to Tester : var messages = db.collection('messages'); messages

Default value not set while using Update with Upsert as true

被刻印的时光 ゝ 提交于 2019-12-23 09:47:42
问题 I have the following model for users: var UserSchema = new mongoose.Schema({ name: String, dob: Date, sex: String, photo: String, email: {type: String, index: {unique: true, required: true}}, created: {type: Date, default: Date.now} }); var User = mongoose.model('Users', UserSchema); As you can see the 'created' field takes a default value of the current date so that it is automatically set when a new user is created. I use the following query when user details are posted: User

Default value not set while using Update with Upsert as true

谁都会走 提交于 2019-12-23 09:47:27
问题 I have the following model for users: var UserSchema = new mongoose.Schema({ name: String, dob: Date, sex: String, photo: String, email: {type: String, index: {unique: true, required: true}}, created: {type: Date, default: Date.now} }); var User = mongoose.model('Users', UserSchema); As you can see the 'created' field takes a default value of the current date so that it is automatically set when a new user is created. I use the following query when user details are posted: User

mongodb update matched doc fail

﹥>﹥吖頭↗ 提交于 2019-12-13 05:15:29
问题 There are 10000 docs import by mongoimport, the _id is 6 length random string {_id:"xxxxxx","u":0,"t":0} because mongoimport can not specifiy data type, so string like "123456" was imported as type int. So I manually delete originals and re-insert them as db.xxx.insert({_id:"123456","u":0,"t":0}) Because the default type of 0 is Double, so I change them to int by: db.xxx.update({},{$set:{u:NumberInt(0)}},false,true) WriteResult({ "nMatched" : 100000, "nUpserted" : 0, "nModified" : 99994 })

mongoDB : updating an objects using dot notation (multi-level object)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:11:31
问题 I have an object with the following form : { "_id": ObjectId("4fa43f4d1cf26a6a8952adf1"), "userId": "1", "facebookId": "1234", "groups": [{ "groupName": "testGroup", "members": [{ "memberFirstName": "userFirstName", "memberLastName": "userLastName", "memberDetails": { "userId": "1", "faceBookId": "1234" } }] }] } It's a collection that holds for each user - its groups, with each group containing the group members... So the "groups" key is an array (or list), and so is the "members" key. (each

$mul nested document field where might not exist

梦想与她 提交于 2019-12-11 06:52:05
问题 I have the following document: { "results" : [ { "name" : "foo", "score" : 10 }, { "name" : "bar" } ] } I want to multiply the score field by 10 , only where it exists. Using just dot notation: { $mul: { 'results.score': NumberInt(10) } } Returns an error: Cannot create field 'score' in element {results: [ { name: "foo", score: 10 }, { name: "bar" } ]} I've tried using the new array filters: { $mul: { 'results.$[result].score': NumberInt(10) } }, { arrayFilters: [{ 'result.grade': { $exists:

Update multiple subdocument without exact sub document level condition

元气小坏坏 提交于 2019-12-08 04:43:24
问题 I have collection with the following document: { "_id" : ObjectId("56cbf9cd75de3ee9057b23c7"), "title" : "Abc", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" : 1, }, { "_id" : "", "message" : "", "active" : 0, "status" : 1, }, { "_id" : "", "message" : "", "active" : 1, "status" : 1, } ], } { "_id" : ObjectId("553744ae75de3eb9128b4568"), "title" : "Jhon", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" : 1, } ] } I need to update all status of