1024programmer Java MongoDB simple add, delete, modify and query

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!'}
 { "id" : "10", "name" : "job", "doc" : "hello world!" }

 #View documentation
 >doc01
 { "id" : "10", "name" : "job", "doc" : "hello world!" }

 #Insert the document into the testtable collection
 > db.testtable.insert(doc01)
 WriteResult({ "nInserted" : 1 })

 #You can also insert documents directly into the collection
 > db.testtable.insert({'id':'11', 'name':'jim', 'doc':'hi world!'})
 WriteResult({ "nInserted" : 1 })

 #When you view the collection again, you will find the newly created collection testtable
 > show tables;
 system.indexes
 testtable

 #Find all documents in the collection testtable
 > db.testtable.find()
 { "_id" : ObjectId("5476cd8e0074a24d1b6eaea7"), "id" : "10", "name" : "job", "doc" : "hello world!" }
 { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }

 #Find the document with id 11
 >db.testtable.find({'id':'11'})
 { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }

 #Update the document with ID 10 and change the name to kiki
 > db.testtable.update({'id':'10'},{'id':'10', 'name':'kiki', 'doc':'hello workd!'})
 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 > db.testtable.find()
 { "_id" : ObjectId("5476cd8e0074a24d1b6eaea7"), "id" : "10", "name" : "kiki", "doc" : "hello workd!" }
 { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }

 #Delete the document with id equal to 10
 > db.testtable.remove({'id':'10'})
 WriteResult({ "nRemoved" : 1 })
 > db.testtable.find()
 { "_id" : ObjectId("5476cdc00074a24d1b6eaea8"), "id" : "11", "name" : "jim", "doc" : "hi world!" }
 >
 

insert( ) function
It is relatively simple to use, just insert it directly or indirectly into a defined document.
update() function

      db.collection.update( criteria, objNew, upsert, multi )
      The update() function accepts the following four parameters:
           criteria: update query conditions, similar to where behind the sql update query.
           objNew: update object and some update operators (such as $, $inc...), etc., can also be understood as the set behind the sql update query
           upsert: This parameter means whether to insert objNew if there is no update record. True means inserting. The default is false and does not insert.
           multi: The default value of mongodb is false. Only the first record found is updated. If this parameter is true, all multiple records found according to the conditions are updated.
 

remove( ) function, dorp( ) function

      Remove data using the remove() function
           If you want to remove the data whose "user_id" is "testuser" in the "userdetails" collection, you can execute the following command:
           >db.userdetails.remove( { "user_id" : "testuser" } )
      Delete all data
           If you want to delete all data in the "userdetails" collection, you can execute the following command:
           >db.userdetails.remove({})
      Use drop() to delete a collection
           If you want to delete the entire "userdetails" collection, including all document data, you can do the following:
           >db.userdetails.drop()
      Use the dropDatabase() function to delete the database
           If you want to delete the entire database data, you can execute the following command:
           >db.dropDatabase()
 

find() function

  Get data from collection
           If you want to read all the data in the collection, you can execute the following command
           >db.userdetails.find();
           Similar to the following SQL query statement:
           Select * from userdetails;
      Read data by specifying conditions
           If we want to read the data whose "education" is "M.C.A." in the collection "userdetails", we can execute the following command:
           >db.userdetails.find({"education":"M.C.A."})
           SQL query statements similar to the following:
           Select * from userdetails where education="M.C.A.";
      Read data through conditional operators
           Conditional operators in MongoDB include:
                (>) Greater than - $gt
                (=) Greater than or equal to - $gte
                () greater than operator - $gt
           If you want to get the data with "age" greater than 22 in the "testtable" collection, you can use the following command:
           >db.testtable.find({age : {$gt : 22}})
           Similar to SQL statement:
           Select * from testtable where age >22;

           MongoDB uses () query operators - $lt and $gt
           If you want to get the data with "age" greater than 17 and less than 24 in the "testtable" collection, you can execute the following command:
           >db.testtable.find({age : {$lt :24, $gt : 17}})

      For more query techniques, please view http://www.w3cschool.cc/mongodb/mongodb-query.html
 

Article source: http://www.xiaomastack.com/2014/11/29/mongodb/

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-simple-add-delete-modify-and-query/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

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