Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. > db.clients.update({ _id: ObjectId("123")}, { $set: { _id: ObjectId("456")}})
  2.  
  3. Performing an update on the path '_id' would modify the immutable field '_id'
  4.  
  5. // store the document in a variable
  6. doc = db.clients.findOne({_id: ObjectId("4cc45467c55f4d2d2a000002")})
  7.  
  8. // set a new _id on the document
  9. doc._id = ObjectId("4c8a331bda76c559ef000004")
  10.  
  11. // insert the document, using the new _id
  12. db.clients.insert(doc)
  13.  
  14. // remove the document with the old _id
  15. db.clients.remove({_id: ObjectId("4cc45467c55f4d2d2a000002")})
  16.  
  17. db.status.find().forEach(function(doc){
  18. doc._id=doc.UserId; db.status_new.insert(doc);
  19. });
  20. db.status_new.renameCollection("status", true);
  21.  
  22. db.someCollection.find().snapshot().forEach(function(doc) {
  23. if (doc._id.indexOf("2019:") != 0) {
  24. print("Processing: " + doc._id);
  25. var oldDocId = doc._id;
  26. doc._id = "2019:" + doc._id;
  27. db.someCollection.insert(doc);
  28. db.someCollection.remove({_id: oldDocId});
  29. }
  30. });
  31.  
  32. {
  33. "_id":ObjectId("5b5ed345cfbce6787588e480"),
  34. "title": "foo",
  35. "description": "bar"
  36. }
  37.  
  38. db.getCollection('myCollection').aggregate([
  39. {$match:
  40. {_id: ObjectId("5b5ed345cfbce6787588e480")}
  41. }
  42. {$project:
  43. {
  44. title: '$title',
  45. description: '$description'
  46. }
  47. },
  48. {$out: 'myCollection'}
  49. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement