mongodb basics - modify field names

    In the past two days, due to the problem of the mongo driver version, the system was incompatible. After filling in the pit for a few days, it was finally completed, but the problem did not fundamentally solve the problem. Later, I decided to carry out a major reconstruction, adding a dao layer and the background mvc architecture. I used to think that a lot of work between dao and service was repeated. After this, I found that the dao layer is indeed necessary, but the data exchange between the service and the dao layer Do not use the format provided by the mongo driver. Before mongo2.x, DBobject was used. After 3.x, a Document was written. Of course, 3.x can still use Dbobject, and also provides a method to convert to Document'. But this is not the official recommendation form. It can only be blamed that the mongo official did not determine the type of data exchange at the beginning, so it is best to use the class that comes with jdk or the class we created for the data exchange between service and dao. It is best not to use third-party classes as a data interchange format.

    If you encounter the data base layer switch, you only need to change the dao layer and write a new one, and all other business layers do not need to be changed. I was too lazy to write dao before, so I operated mongo directly in the service layer, which caused the mongo switch. Modify many service implementations.

    Finally, let's talk about a mongo operation that I used recently. I searched the Internet and found a solution. In fact, the function to be implemented is to rename the field. I didn't notice that an operator in the official document can achieve this function, so I modified it myself, simple violence, first set the value of the original field to the new field, and then delete the original field, and when you see the official $rename description, the internals are not worse than this implementation.

    1. Copy the value of a field to a new field:

// The third parameter false indicates whether the document does not exist or not, the default is false
// The fourth parameter true indicates whether to update all documents that meet the condition, the default is false
db.coll_name.find({}).forEach(
     function(doc) {
        db.coll_name.update({"_id" : doc._id}, {"set" : {"new_filed" : doc.old_field}}, false, true);
     }
);

//

    2. Delete fields

// The syntax for deleting a field is
{ $unset: { <field1>: "", ... } }

// For example, delete a name field
db.coll_name.update({"_id" : 1}, {"$unset": { "name": ""}}, false, false);
db.coll_name.update({"_id" : 1}, {"$unset": { "name": "无所谓"}}, false, false);
// Both of these methods are available, that is to say, the value you set for the field to be deleted will not affect the operation of deleting the field

    3. Modify the field name

db.coll_name.update({}, {"$rename": { "old_field": "new_field"}}, false, true);

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326392283&siteId=291194637