nodejs connects to mongodb database to implement addition, deletion, modification and query

Preparation 1. Install mongodb through npm command 2. Install mongodb database CRUD operation Before this, you should have some understanding of the MongoDB database and some of its add, delete, check and modify commands. 1. Add var MOngoClient= require(“mongodb”).MongoClient; var DB_URL = “mongodb://localhost:27017/chm”; function insertData(db) { var devices = db.collection('vip'); var data = {“name”:”node”,”age”:22,”addr”:”nb”,”addTime”:new Date()}; devices.insert(data,function(error, result){ if(error) { console.log('Error:'+ error); }else{ console.log(result.result.n); } db.close(); }); } MongoClient.connect(DB_URL, function(error, db){ console.log('Connection successful!'); insertData(db); }); 2. Find var mOngodb= require('mongodb') var MOngoClient= require('mongodb').MongoClient; var DB_CONN_STR = 'mongodb://localhost:27017/chm'; var selectData = function(db, callback) { //Connect to table var collection = db.collection('vip'); //Query data var whereStr = {“name”:'node'}; collection.find(whereStr,function(error, cursor){ cursor.each(function(error,doc){ if(doc){ //console.log(doc); if (doc.addTime) { console.log(“addTime: “+doc.addTime); } } }); }); } MongoClient.connect(DB_CONN_STR, function(err, db) { console.log(“Connection successful!”); selectData(db, function(result) { console.log(result); db.close(); }); }); 3. Update var MOngoClient= require(“mongodb”).MongoClient; var DB_URL = “mongodb://localhost:27017/chm”; MongoClient.connect(DB_URL, function(error, db){ console.log(“Connection successful!”); updateData(db); }); function updateData(db) { var devices = db.collection('vip'); var whereData = {“name”:”node”} var updateDat = {$set: {“age”:26}}; //If $set is not used, replace the entire data devices.update(whereData, updateDat, function(error, result){ if (error) { console.log('Error:'+ error); }else{ console.log(result); } db.close(); }); } 4. Delete var MOngoClient= require('mongodb').MongoClient; var DB_URL = “mongodb://localhost:27017/chm”; MongoClient.connect(DB_URL, function(error,…

MongoDB (6) java operation mongodb addition, deletion, modification and query

We already know the code of Java operating mysql database very well, such as addition, deletion, modification and query. Java also performs similar operations on mongodb database. First, connect to the database, and then perform operations. First we enter the admin database, and then create our own database testMongoDb. After entering the admin database, we can directly enter testMongoDb, because users can We already know the code of Java operating mysql database very well, such as adding, deleting, modifying and checking. Java also performs similar operations on mongodb database. The database connection is first, and then the operation is performed. First we enter the admin database, and then create our own database testMongoDb. After entering the admin database, we can directly enter testMongoDb, because the user can enter the system database, which is the super administrator. After use testMongoDb, set the user for the database name and password, db.addUser('root','root'). In this way, we can connect to the database in the program and implement addition, deletion, modification and query. The code is as follows. The code is as follows: package com.mkyong.core; import java.net.UnknownHostException; import java.util.Date; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * Java +…

PHP simple method to operate MongoDB (installation, addition, deletion, modification and query), mongodb addition and deletion_PHP tutorial

PHP simple method to operate MongoDB (installation, addition, deletion, modification and query), mongodb addition and deletion_PHP tutorial

How to simply operate MongoDB with PHP (installation, addition, deletion, modification and query), add and delete mongodb This article describes the simple method of operating MongoDB with PHP. Share it with everyone for your reference, the details are as follows: If PHP operates MongoDB, first download the MongoDB expansion package from the Internet, https://github.com/mongodb/mongo-php-driver/downloads, and select the corresponding expansion package. I downloaded this and decompressed it. VC6 is suitable for apache, VC9 is suitable for IIS, and ts (thread safe) means that PHP runs in the form of a module. Then put the php_mongo.dll in the ext folder in PHP, then add extension=php_mongo.dll to PHP.INI and restart apache. Now the PHP extension MongoDB package is installed. For information on querying some MongoDB functions, please refer to the manual http://us.php.net/manual/en/class.mongocollection.php PHPDataBase; $collection = $db->PHPCollection; /*——————————– * delete *——————————– $collection->remove(array(“name” => “xixi111”)); */ /*——————————— * insert *——————————– for($i = 0;$i “xixi”.$i,”email” => “673048143_”.$i.”@qq.com”,”age” => $i*1+20); $collection->insert($data); } */ /*———————————- * Find *——————————– $res = $collection->find(array(“age” => array(‘$gt’ => 25,’$lt’ => 40)),array(“name” => true)); foreach($res as $v) { print_r($v); } */ /*———————————- * renew *——————————– $collection->update(array(“age” =>22),array(‘$set’ => array(“name” => “demoxixi”))); */ ?> Readers who are interested in more PHP-related content can check…

pythonDjango connects to MySQL database for addition, deletion, modification and query

1. Download and install the MySQLdb class libraryhttp://www.djangoproject.com/r/python-mysql/2. Modify settings.py to configure data attributes The code is as follows: DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.mysql’, # Add ‘postgresql_psycopg2’, ‘mysql’, ‘sqlite3’ or ‘oracle’. ‘ NAME’: ‘djangodb’, # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: ‘USER’: ‘root’, ‘PASSWORD’: ‘root ‘, ‘HOST’: ‘127.0.0.1’, # Empty for localhost through domain sockets or ‘127.0.0.1’ for localhost through TCP. ‘PORT’: ‘3306’, # Set to empty string for default. }} After modification, enter DOS and enter the project directory to execute the python manage.py shell command to start the interactive interface and enter the code to verify whether the database configuration is successful. If no error is reported, it is successful! The code is as follows: >> > from django.db import connection>>> cursor = connection.cursor() 3. Create a Django appA project contains One or more such apps. An app can be understood as a collection of functions. For example, the product management module includes functions such as adding, deleting, and checking. Product management can be called an app. Each Django app has independent models, views, etc., which is easy to transplant and reuse. DOS enters the…

mongodb basic addition, deletion, modification and query

To learn a database, the first thing you need to know is how to connect to the database, how to perform basic additions, deletions, modifications and queries, and you can learn other advanced functions slowly. The following mainly explains the basic addition, deletion, modification and query of mogodb 1. Create a database: use mkyongdb (jump to this database if there is one, create and jump if not) Display all databases: show dbs 2. Create a collection ( To learn a database, the first thing you need to know is how to connect to the database, how to perform basic additions, deletions, modifications and queries, and you can learn other advanced functions slowly. The following mainly explains the basic addition, deletion, modification and query of mogodb 1. Create a database: use mkyongdb (if there is one, jump to this database, if not, create and jump) Show all databases: show dbs 2. Create a collection (equivalent to a table in a relational database): Collection operations: (1) Insert data db.users.insert({‘name’:’xumingxiang’,’sex’:’man’}) creates a collection of users Show all collections under the current database: show collections Display all data documents under the users collection: db.users.find() Display the number of records in the collection: db.users.count() (2)…

Java operation mongodb: basic addition, deletion, modification and query

Java code for operating mongodb, including basic add, delete, modify and query operations to obtain the database connection tool class package com.liuc.db;import java.net.UnknownHostException;import com.mongodb.DB;import com. mongodb.DBCollection;import com.mongodb.Mongo;/** * * @brief MongoDBUtil.java operation Java code for operating mongodb, including basic add, delete, modify and query operations Get the database connection tool class package com.liuc.db; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; /** * * @brief MongoDBUtil.java operating mongodb tool class * @attention Notes on usage * @author liuchao * @date 2013-12-30 * @note begin modify by Modifier Modification time Summary of modification content */ public class MongoDBUtil { /** * * \brief Obtain database connection without authentication * @return * Precautions for using @attention method * @author liuchao * @date 2013-12-30 * @note begin modify by Modifier Modification time Summary of modification content */ public static DBCollection getDBConnectionWithoutAuth(String colName){ try { Mongo mOngo= new Mongo(“localhost”, 27017); DB db = mongo.getDB(“liuchao”); return db.getCollection(colName); } catch (UnknownHostException e) { e.printStackTrace(); return null; } } /** * Obtain the database connection that requires authentication Start login password authentication: Log in to the database and add users use db.addUser('user1','pwd1'); Restart the server and enable the authentication service mongod –auth –dbpath=D:\mongodb\db Next, you will need…

MongoDB (6) java operation mongodb addition, deletion, modification and query

We already know the code of Java operating mysql database very well, such as addition, deletion, modification and query. Java also performs similar operations on mongodb database. First, connect to the database, and then perform operations. First, we enter the admin database, and then create our own database testMongoDb. After entering the admin database, we can directly enter testMongoDb, because users can java operate the mysql database code. We already know everything about it, adding, deleting, modifying, and checking. Java also performs similar operations on the mongodb database. The database is connected first, and then the operation is performed. First we enter the admin database, and then create our own database testMongoDb. After entering the admin database, we can directly enter testMongoDb, because users can enter the system database and are super administrators. After use testMongoDb, Set the user name and password for the database, db.addUser('root','root'), so that we can connect to the database in the program and implement additions, deletions, modifications and queries. The code is as follows. package com.mkyong.core; import java.net.UnknownHostException; import java.util.Date; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * Java + MongoDB Hello world Example * */ public class App…

nodejs connects to mongodb database to implement addition, deletion, modification and query

Preparation 1. Install mongodb through npm command 2. Install the mongodb database. I will not introduce it in detail here. The installation URL is: https://www.jb51.net/article/82522.htm CRUD operations Before this, you should have some understanding of the MongoDB database and some of its add, delete, check and modify commands. 1. Add var MOngoClient= require(“mongodb”).MongoClient; var DB_URL = “mongodb://localhost:27017/chm”; function insertData(db) { var devices = db.collection(‘vip’); var data = {“name”:”node”,”age”:22,”addr”:”nb”,”addTime”:new Date()}; devices.insert(data,function(error, result){ if(error) { console.log(‘Error:’+ error); }else{ console.log(result.result.n); } db.close(); }); } MongoClient.connect(DB_URL, function(error, db){ console.log(‘Connection successful!’); insertData(db); }); 2. Search var mOngodb= require(‘mongodb’) var MOngoClient= require(‘mongodb’).MongoClient; var DB_CONN_STR = ‘mongodb://localhost:27017/chm’; var selectData = function(db, callback) { //Connect to table var collection = db.collection(‘vip’); //Query data var whereStr = {“name”:’node’}; collection.find(whereStr,function(error, cursor){ cursor.each(function(error,doc){ if(doc){ //console.log(doc); if (doc.addTime) { console.log(“addTime: “+doc.addTime); } } }); }); } MongoClient.connect(DB_CONN_STR, function(err, db) { console.log(“Connection successful!”); selectData(db, function(result) { console.log(result); db.close(); }); }); 3.Update var MOngoClient= require(“mongodb”).MongoClient; var DB_URL = “mongodb://localhost:27017/chm”; MongoClient.connect(DB_URL, function(error, db){ console.log(“Connection successful!”); updateData(db); }); function updateData(db) { var devices = db.collection(‘vip’); var whereData = {“name”:”node”} var updateDat = {$set: {“age”:26}}; //If $set is not used, replace the entire data devices.update(whereData, updateDat, function(error, result){ if (error) { console.log(‘Error:’+ error); }else{ console.log(result); } db.close();…

Sample code for springboot integrated mongodb addition, deletion, modification and query

Add dependencies org.springframework.boot spring-boot-starter-data-mongodb properties configuration spring.data.mongodb.host=127.0.0.1 spring.data.mongodb.port=27017 spring.data.mongodb.database=enterprise_dc_db spring.data.mongodb.username=enterprise_dc_dba spring.data.mongodb.password=123456 server.port=8081 Create entity class @Document is the mongodb collection name package com.example.springbootmongodb.domian; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = “mydb”) public class Users { private String userId; private String name; private Integer age; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } controller package com.example.springbootmongodb.controller; public class ResultObject { private int code; private String desc; private Object data; public ResultObject() { } public ResultObject(int code) { this.code = code; } public ResultObject(int code, Object data) { this.code = code; this.data = data; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } } package com.example.springbootmongodb.controller; import com.example.springbootmongodb.domian.Users; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.web.bind.annotation.*; import java.util.List; @RequestMapping(“/mongo”) @RestController…

Example of webpack4+express+mongodb+vue implementing addition, deletion, modification and query

Before explaining, let’s take a look at the effect as follows: 1) The effect of the entire page is as follows: 2) The effect of adding new data is as follows: 3) The new addition is successful as follows: 4) The effect of editing data is as follows: 5) The result of successful editing is as follows: 6) The effect of deleting data is as follows: 7) The effect of successful deletion is as follows: 8) The query results are as follows: With the above effect, we will still do the same as before. Let’s first look at the structure of our entire project as follows: ### The directory structure is as follows: demo1 # project name | |— dist # Directory file generated after packaging | |— node_modules # All dependent packages | |—-database # Database related file directory | | |—db.js # Database connection operation of mongoose class library | | |—user.js # Schema Create model | | |—addAndDelete.js # Add, delete, modify and check operations | |— app | | |—index | | | |– views # Store all vue page files | | | | |– list.vue # List data | | | | |– index.vue…

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
首页
微信
电话
搜索