Add, delete, modify, and query array elements and embedded documents in the MongoDB database

For example, I have a user class, which contains a label attribute. This label is an array, and the elements in the array are embedded documents. The format is as follows: { “_id” : “195861”, “tags” : [                                               { “tagId” : NumberLong(766), “optDate” : ISODate(“2013-08-12T15:21:02.930Z”), “enable” : true               },                                               { “tagId” : NumberLong(778), “optDate” : ISODate(“2013-08-12T15:21:02.930Z”), “enable” : true           } ] } Next, add, delete and check tags in this document. Spring mongodb is used here. MongoTemplate class inside. Here I abstract the embedded documents in tags into the Tag class. Code deletion and modification itself contains query methods, so no query methods are written. /** * * @author zhangdonghao * */ @Component(“UserrTagServiceImpl”) public class UserrTagServiceImpl implements UserrTagService { /** * Mongo DB Spring Template */ @Resource protected MongoTemplate mOngoTemplate= null; public UserrTagServiceImpl() { } /** **Add an element to the tags array */ @Override public Response 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(0); } Return new Response(1); }…

How to use $http to add, delete, modify and query MongoLab data tables in AngularJS_AngularJS

Main page: Load Course Add New Course Above, the content display of course_list.html, add_course.html and edit_course.html displayed on the page is related to the toggleAddCourseView and toggleEditCourseView values, and the toggleAddCourseView and toggleEditCourseView values ​​will be controlled by methods. Create databases and tables on Mongolab → https://mongolab.com → Register → Login → Create new → Select Single-node Check the Sandbox and enter the Database name as myacademy. → Click on the newly created Database → Click Add collection The name is course → Click on the course collection. → Click add document multiple times to add multiple pieces of data Controller $scope.courses = []; var url = “https://api.mongolab.com/api/1/databases/my-academy/collections/course?apiKey=myAPIKey”; var cOnfig= {params: {apiKey: “…”}}; $scope.toggleAddCourseNew = false; $scope.toggleEditCourseView = false; //list $scope.loadCourses = function(){ $http.get(url, config) .success(function(data){ $scope.courses = data; }); } //Add to $scope.addCourse = function(course){ $http.post(url, course, config) .success(function(data){ $scope.loadCourses(); }) } //Show changes $scope.editCourse = function(course){ $scope.toggleEditCourseView = true; $scope.courseToEdit = angular.copy(course); } //Revise $scope.updateCourse = function(courseToEdit){ var id = courseToEdit._id.$oid; $http.put(url + “/” + id, courseToEdit, config) .success(fucntion(data){ $scope.loadCourses(); }) } //delete $scope.delteCourse = function(course){ var id = course._id.$oid; $http.delete(url+ “/” + id, config) .success(function(data){ $scope.loadCourses(); }) } $scope.toggleAddCourse = function(flag){ $scope.toggleAddCourseView = flag; } $scope.toggleEditCourse =…

MongoDB performs add, delete, modify and query operations on array elements and embedded documents

MongoDB performs add, delete, modify and query operations on array elements and embedded documents

The following is to add, delete and check the tags in this document. The MongoTemplate class in spring mongodb is used here. Here I abstract the embedded documents in tags into T For example, I have a user class, which contains a label attribute. This label is an array, and the elements in the array are embedded documents. The format is as follows: { “_id” : “195861”, “tags” : [ { “tagId” : NumberLong(766), “optDate” : ISODate(“2013-08-12T15:21:02.930Z”), “enable” : true }, { “tagId” : NumberLong(778), “optDate” : ISODate(“2013-08-12T15:21:02.930Z”), “enable” : true } ] } The following is to add, delete and check the tags in this document. The MongoTemplate class in spring mongodb is used here. Here I abstract the embedded documents in tags into the Tag class. Code deletion and modification itself contains query methods, so no query methods are written The code is as follows: /** * * @author zhangdonghao * */@Component(“UserrTagServiceImpl”)public class UserrTagServiceImpl implements UserrTagService { /** * Mongo DB Spring Template */@Resourceprotected MongoTemplate mOngoTemplate= null; public UserrTagServiceImpl() { }/****Add an element to the tags array*/@Overridepublic Response 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…

MongoDB simple add, delete, modify and query

Insert, remove, drop, update, find, show dbs, show tables will first explain the usage method with a simple practical operation, and then analyze it in more detail. Under Linux, you can enter a control interface similar to mysql by executing the mongo command that comes with mongodb. Mongodb’s database, collections, and documents are similar to the database, tables, and records in mysql The use of insert, remove, drop, update, find, show dbs, and show tables will first be explained with a simple practical operation, and then analyzed in more detail. Under Linux, you can enter a control interface similar to mysql by executing the mongo command that comes with mongodb. Mongodb’s database, collections, and documents are similar to the concepts of databases, tables, and records in mysql. Here are some useful information. #View database > show dbs; admin (empty) local 0.078GB myinfo 0.078GB #Switch database. If the database does not exist, it will be automatically created when the first record is added. > use myinfo switched to db myinfo #View the collection and automatically create the collection when adding documents to a collection that does not exist > show tables; system.indexes #Define a document > doc01={‘id’:’10’, ‘name’:’job’, ‘doc’:’hello world!’} {…

Getting Started with MongoDB – Add, Delete, Modify and Query – mysql tutorial

Getting Started with MongoDB – Add, Delete, Modify and Query – mysql tutorial

mongodb started successfully, and then it is time to perform a series of operations. Let’s open another cmd and enter the [mongo] command to open the shell, which is the client of mongodb. The default connection is the test database. I set the collection (table) here to student. Figure 1: 1. Add insert syntax: db.set.insert({Col1: column value 1, Col2: column value 2,, Coln: column value n} Mongodb started successfully, and then it is time to perform a series of operations. Let’s open another cmd and enter the [mongo] command to open the shell, which is the client of mongodb. The default connection is the “test” database. I set the collection (table) here to student. Figure 1: 1. Add insert Syntax: db.set.insert({“Col1″:”Column value 1″,”Col2″:”Column value 2″,…,”Coln”:”Column value n”}) 2. Find find 2.1 All queries Syntax: db.collection.find() 2.2 Conditional query Syntax: db.set.find({“Col1″:”Column value 1″,”Col2″:”Column value 2″,…,”Coln”:”Column value n”}) Operation example: Add+Find Note: The “_id” field is the GUID added to us by the database by default to ensure the uniqueness of the data. 3.Modify 3.1 All modifications update Syntax: db.set.update({“Col”:”Column value”},{“Col2″:”Column value 2″,…,”Coln”:”Column value n”}) Note: The first parameter {…} is the “search condition”, and the second parameter {…} is the “value…

MongoDB (3) Add, delete, modify and query

MongoDB (3) Add, delete, modify and query

MongoDB is a non-relational database, and there is a big difference between the addition, deletion, modification and query of traditional databases. Here is just an outline of the knowledge points. When it is actually used, we can Baidu for detailed usage. Can. Let’s look at the major aspects first: 1. I won’t go into too much detail about the insertion and deletion inside. Relatively speaking, there are relatively few knowledge points. Let’s take a look at the updates first, As a non-relational database, MongoDB is very different from the addition, deletion, modification and search of traditional databases. Here is just an outline of the knowledge points. When it is actually used, we can search the detailed usage on Baidu. Let’s look at the major aspects first: First, I won’t go into too much detail about the insertion and deletion. There are relatively few knowledge points. Let’s take a look at the updates first. Common operations are still very useful and we need to use them flexibly according to the actual situation. Here are just some knowledge points. Baidu needs examples to learn how to use them: 2. The same is true for queries. Take a look at the summary of…

Introduction to MongoDB Learning (3): MongoDB Add, Delete, Check and Modify

For novices like us, the most important thing is not the management of the database, nor the performance of the database, nor the expansion of the database, but how to make good use of this database, that is, a database provides The most core function is addition, deletion, checking and modification. Because MongoDB stores data in document mode, when operating its data, it is also based on documents. For novices like us, the most important thing is not the management of the database, nor the performance of the database, nor the expansion of the database, but how to make good use of this database, which is the core function provided by a database. , add, delete, check and modify. Because MongoDB stores data in document mode, when operating its data, it is also based on documents. Then our implementation of adding, deleting, checking and modifying is also based on documents. Students who don’t know what documents are can take a look at the basic concepts introduced in the previous article. 1. Insert document The basic method of inserting documents into a MongoDB collection is insert: single insert > document = {key : value} > db.collectionName.insert(document) Batch insert > documents =…

Mongodb write, delete, update

1. Mongodb creates databases and collections The creation of mongodb databases and collections is implicit. This means that there is no need to write a separate create database statement. Just use the use keyword directly. Run under bin/mongo shell: use test; This will generate a test database. If you leave without writing, the system will automatically delete it. Sets are also implicit and do not need to be specifically referred to 1. Mongodb creates databases and collections Creation of mongodb databases and collections is implicit. This means that there is no need to write a separate create database statement. Just use the use keyword directly. Run under bin/mongo shell: use test; This will generate a test database. If it is not written and left, the system will automatically delete it. Collections are also implicit. You don’t need to specify them. If you directly insert a document, a collection will be generated. 2. Document writing Insert using insert: db.user.insert({“name” : “gang”}); user is the collection name, and a piece of data is written. 3. Document deletion Document deletion uses the remove keyword. db.user.remove(); Delete all data under user. If you specify to delete data under specific conditions, you need to add…

MongoDB add, delete, modify

Introduction to MongoDB driver types 1. MongoDB official driver: mongo-csharp-driver, download address: https://github.com/mongodb/mongo-csharp-driver/downloads 2. Third-party driver samus , this is a frequently used driver with a relatively fast update frequency. In addition to supporting general operations, the samus driver also supports Linq Introduction to MongoDB driver types 1. MongoDB official driver: mongo-csharp-driver, download address: https://github.com/mongodb/mongo-csharp-driver/downloads 2. The third-party driver samus is a commonly used driver with a relatively fast update frequency. In addition to supporting general operations, the samus driver also supports Linq and Lambda expressions. Download address: https://github.com/samus/mongodb-csharp. The operation process of the two mongodb drivers on the mongodb database is basically the same, but there are differences in the implementation methods. For example, the samus driver not only supports general formal operations, but also supports Linq and Lambda expressions. Use MongoDB official driver to operate the database Unzip the package and get the following two files: MongoDB.Bson.dll: Serialization, Json related MongoDB.Driver.dll: mongodb driver Add references and introduce the above two DLLs into the project Introducing namespaces into code using MongoDB.Bson; using MongoDB.Driver; Obtain database connection service string cOnnectionString= ” mongodb://localhost ” ; //mongodb://[username:password@]hostname[:port][/[database][?options]] MongoServer server = MongoServer.Create(connectionString); // Connect to a MongoServer Get a reference to the specified…

Detailed examples of operating MongoDB database with PHP (add, delete, modify, check) (6)

PHP operates mongodb: PHP needs to install modules to operate mongodb The official website can be downloaded: http://pecl.php.net/package/mongo download mongodb Set the startup mode to user authorization The php manual does not have some user authorization methods to log in: conn.php <?php $cOnn= new Mongo( “mongodb://user1:123456@localhost:27017/test”); //User authorization to link to mongodb test database $db = $conn->test; ?> find.php <?php include “conn.php”; $c1 = $db->c1; //Operation c1 collection / /Because json cannot be used directly in php //db.c1.find({name:”user1″}); cannot be played like this //{name:”user1″} == array(” name”=>”user1″) use this form //[1,2] == array(1,2); //{} == array() $arr=array(); $rst = $c1->find($arr); foreach($rst as $val){ echo “ ” ; print_r($val[‘name’]); //If you get the id, you get “_id” } Example 2: Query by specified value /> $arr = array(“name”=>”user1″); //Query nam=user1 $rst = $c1->find($arr); foreach($rst as $val){ echo ” “; $fis = $val[‘_id’]; print_r($val); echo “”; // You will find that fid becomes a string when passed to user.php. How to solve it? //user.php Check the data corresponding to mongodb according to _id <?php include “conn.php”; $c1 = $db->c1; /> $oid= new MongoId($_GET[‘fid’]); Use this to convert var_dump($oid); //It is still Object, otherwise it will be string type $arr = array(“_id”=>”$oid”); $rst…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索