How does mongodb add, delete, modify and check array elements and embedded documents?

  node.js, python

For example, I found the java version on the Internet, but I don’t know java. The specific address is as follows
https://my.oschina.net/132722 …
Now that you want to operate in python or js, how can you add, delete, modify, and check in an embedded array?

The problem is reproduced like this. I hope the great god can give me some advice.

For example, I have a user class that contains a tag attribute. The tag is an array, and the elements in the array are embedded documents in the following format:

<!  -- lang: js -->
 bracket
 "_id" : "195861",
 "tags" : [
 bracket
 "tagId" : NumberLong(766),
 "optDate" : ISODate("2013-08-12T15:21:02.930Z"),
 "enable" : true
 },
 bracket
 "tagId" : NumberLong(778),
 "optDate" : ISODate("2013-08-12T15:21:02.930Z"),
 "enable" : true
 bracket
 bracket
 
 bracket

The tag in this document is added, deleted and checked below. The MongoTemplate class in spring mongodb is used here. I abstract the embedded documents of tags inside into Tag classes here. Code deletion and modification itself include query methods, so query methods are not written

The code is as follows:

<!  -- lang: java -->
 /**
 *
 * @author zhangdonghao
 *
 */
 @Component("UserrTagServiceImpl")
 public class UserrTagServiceImpl implements UserrTagService {
 
 /**
 * Mongo DB Spring Template
 */
 @Resource
 protected MongoTemplate mongoTemplate = null;
 
 public UserrTagServiceImpl() {
 
 bracket
 /**
 * * Add an element to tags array
 */
 @Override
 public Response<Integer> addTag(String id, Long tagId) {
 
 try {
 Tag tag = new Tag(tagId);
 tag.setOptDate(new Date());
 tag.setEnable(true);
 Query query = Query.query(Criteria.where("_id").is(id));
 Update update = new Update();
 update.addToSet("tags", tag);
 mongoTemplate.upsert(query, update, User.class);
 } catch (Exception e) {
 return new Response<Integer>(0);
 bracket
 return new Response<Integer>(1);
 bracket
 
 /**
 * * Modify embedded documents in tags array to specify the value of an element
 */
 @Override
 public Response<Integer> disableTag(String id, Long tagId) {
 
 try {
 Query query = Query.query(Criteria.where("_id").is(id)
 .and("tags.tagId").is(tagId));
 Update update = new Update();
 update.set("tags.$.enable", false);
 mongoTemplate.updateFirst(query, update, User.class);
 } catch (Exception e) {
 return new Response<Integer>(0);
 bracket
 return new Response<Integer>(1);
 bracket
 /**
 * * Deletes the embedded document specified in the tags array
 */
 @Override
 public Response<Integer> removeTag(String id, Long tagId) {
 
 try {
 Query query = Query.query(Criteria.where("_id").is(id)
 .and("tags.tagId").is(tagId));
 Update update = new Update();
 update.unset("tags.$");
 mongoTemplate.updateFirst(query, update, User.class);
 } catch (Exception e) {
 return new Response<Integer>(0);
 bracket
 
 return new Response<Integer>(1);
 bracket
 
 
 public MongoTemplate getMongoTemplate() {
 return mongoTemplate;
 bracket
 
 public void setMongoTemplate(MongoTemplate mongoTemplate) {
 this.mongoTemplate = mongoTemplate;
 bracket
 bracket

I don’t know if the subject has solved the problem. If you use mongoose, it would look like this:

1. Add/Remove: Use the $ PUSH and $ PULL operation symbols of the array

2, change, use $update and $ locator

3. The query uses aggregate, match, unwind

The general code can be written as follows:

var mongoose = require('mongoose');
 
 mongoose.connect('mongodb://localhost:27017/test');
 
 var db = mongoose.connection;
 
 db.on('error',function (err) {
 Log ('mongoose connectionerror:' plus err);
 });
 
 db.once('open', function() {
 console.log('Mongoose connection connected!'  );
 });
 
 var Schema = mongoose.Schema;
 
 var userSchema = new Schema({
 _id : String,
 tags: [{ _id: false, tagID: Number, optDate: Date, enable: Boolean }]
 });
 
 var User = mongoose.model('User', userSchema,'user');
 
 //Query
 
 User.aggregate({ $project: { _id : 1, tags: 1 } }).unwind('tags').exec(function (err, users) {
 if (err) return console.error(err);
 console.log(users);
 });
 
 //Insert
 
 User.update({ "_id" : "195861"}, { $push : { tags: {tagID : 888, optDate : Date("2016-08-12T15:21:02.930Z"), enable : false}}},function(err,result){
 if (err) return console.error(err);
 console.log(result);  });
 
 //delete
 
 User.update({ "_id" : "195861"}, { $pull : { tags: {tagID : 779}}},function(err,result){
 if (err) return console.error(err);
 console.log(result);  });
 
 //Update
 
 User.update(
 bracket
 "_id" : "195861",
 "tags.tagID" : 888
 },
 bracket
 $set: {
 "tags.$" : {
 'tagID" : 333,
 "optDate" : new Date(),
 "enable" : true}
 bracket
 },function(err,result){
 if (err) return console.error(err);
 console.log(result);  });

Love MongoDB! Have Fun!