Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Getting started with the basic operation of MongoDb

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Introduction to 1.MongoDb

Mongodb is an open source database system based on distributed storage, also known as document database, which can store data as a document. The data structure is composed of key-value pairs (key= > value), and the stored documents are similar to JSON objects (binary of BSON- > JSON).

Features: the internal execution engine is the JS interpreter, which stores the document as a BSON structure, converts it to JS objects when querying, and can operate through the familiar JS syntax.

Traditional database: structured data, after the table structure is determined, the content of each row must be in accordance with the table structure, that is, the number and type of columns are the same.

Document database: each document under the table can have its own unique structure or properties and values.

Installation of 2.MongoDb (detailed installation steps under centos7)

1) download the mongodb installation package first:

# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.4.tgz

2) go to the downloaded directory and extract the installation package of mongodb to the specified directory:

# tar-xvf mongodb-linux-x86_64-rhel70-3.4.4.tgz-C / usr/local

# cd / usr/local

Rename the directory where MongoDb was extracted

# mv mongodb-linux-x86_64-rhel70-3.4.4 mongodb#cd mongodb

4) start MongoDb

# mkdir-p / home/mongodata/ / var/log/mongolog/#./bin/mongod-dbpath / home/mongodata/-logpath / var/log/mongolog/mongo.log-fork-port 27017

Description of startup parameters:

-- dbpath data storage directory

-- logpath log storage directory

-- port operation port

-- fork runs in the background

5) Connect to MongoDb through the client

#. / bin/mongo

Note: in some older versions, MongoDb takes up a lot of disk space when it starts, and it may take up 3-4G space after startup. If the virtual machine space is too small, it will not be able to start, but it has an option-smallfiles can reduce the boot space to about 400m.

Concrete operation example of 3.mongodb

1. How to get started with mongodb

1) query all library lists

> show dbs

2) switch databases (select the database to use)

> use local

3) create a database

(note: because the mongodb database is implicitly created, there is no direct way to create the database, but you can directly use use to switch to a library that does not exist before, and the current database will be created automatically when the table (collections) is created.)

> use shop

(assuming that the shop library does not exist, you can also use use directly)

> db.createCollection ('user')

(you can create a collections (or create a table) directly under a library that does not exist.)

> show dbs

Then when you look at the library, you will find that the library shop has been created by default.

4) create a table (collections)

> db.createCollection ('user')

Note: tables (that is, collections) can also be created implicitly, and take the example above: if a goods table (collections) does not exist in the shop library, we can still insert data in the following way

> db.goods.insert ({_ id:1,name:'pipixia',price:52.10})

Then when we look at the collections in the shop library, we find that the goods has been created automatically.

5) insert a single document (data) into collections (table) (the inserted data is in json format)

> db.user.insert ({name:' Zhang San', age:18})

6) find the contents of collections (table)

> db.user.find ()

7) Delete collections (table) ()

> db.user.drop ()

8) Delete the database (databases)

> db.dropDatabase ()

Second, the basic operation of mongodb (add, delete, change, check) CURD

1. Add operation: insert

Note: mongodb stores documents, and so-called documents here are actually objects in json format

1) add a single document

> db.collectionName.insert ({sn:'001',name:'xiaoming'})

2) add a single document and specify id

> db.collectionName.insert ({_ id:2,sn:'002',name:'xiaohong'})

3) add multiple documents (because json is an object, js has the concept of an array, you just need to put multiple objects into an array)

> db.collectionName.insert ([{sn:'003','name':'zhangfei'}, {sn:'004','name':'guanyu'}, {sn:'004','name':'liubei'}])

two。 Delete operation: remove

Syntax: db.collectionName.remove (query expressions, options)

The option refers to {justOne:true/false}, whether to delete a row. The default is false.

Note:

1. The query expression is still a json object

two。 The row matched by the query expression will be deleted

3. If there is no query expression, all documents in collections will be deleted

1) Delete the specified json document (object in json format)

For example: delete the json object document with the value of sn attribute 002 in the collectionName table

> db.collectionName.remove ({sn:'002'})

2) if the query expression is empty, as follows

> db.collectionName.remove ()

All documents in the library will be deleted. Be careful when using them

3) if we want to delete a row of data that matches the query expression, we can use the "options" feature mentioned above, such as

> db.collectionName.remove ({"name": "zhangsan"}, true)

Only one of the document data that name conforms to zhangsan will be deleted.

3. Change operation: update

Syntax: db.collectionName.update (query expressions, new values, options)

1) query expression refers to who needs to be modified, new value refers to what to change, and option refers to optional parameters.

For example:

> db.collectionName.update ({name:'zhangfei'}, {name:'zhangfeifei'})

Check the results of the changes

> db.collectionName.find ()

You will find that only the id and name columns are left in the modified document, while the sn column is missing

Reason: the new document directly replaces the old one, not modifies it

2) but we can use $set to specify a column to be modified

> db.collectionName.update ({name:'guanyu'}, {$set: {name:'guanyunchang'}})

Assignment expression when modified

$set modifies the value of a column

$unset deletes a column

$rename renames a column

$inc increases the value of a column

3) A complex update operation

Insert a piece of data first

> db.collectionName.insert ({name:'caocao',age:40,sex:'m',addr:'wei')

Modify this piece of data

> db.collectionName.update ({name:'caocao'}, {$set: {name:'caoaman'}, $unset: {addr:1}, $rename: {sex:'gender'}, $inc: {age:10}})

4) the function of the third optional parameter option:

{upsert:true/false,multi:true/false}

Upsert means that there is no matching row, then the row is inserted directly. (similar to replace in mysql)

For example:

> db.collectionName.update ({_ id:100}, {name:'liubei'}, {upsert:true})

If there is no document with a _ id of 100, insert it directly.

Note: if there is neither a document with a _ id of 100 nor an optional parameter for upsert, the new operation will not affect the document in the table.

Multi refers to whether to change multiple lines or not.

That is, if the query expression matches multiple rows, only one row will be changed by default, and you need to use this option if you want to modify multiple rows.

For example:

> db.collectionName.update ({sex:'m'}, $set: {sex:'w'}, {multi:true})

Will change all sex=m documents in collectionName to sex=w

4. Check operation: find,findOne

Syntax: db.collection.find (query expression, query column)

For example:

1) query all documents

> db.collectionName.find ()

2) query the sex properties of all documents (columns)

> db.collectionName.find ({}, {sex:1})

3) query the sex attribute of all documents, not the _ id attribute

> db.collectionName.find ({}, {sex:1,_id:0})

Note: both additions, deletions, changes and queries are inseparable from a query expression. The above examples are relatively simple to get started.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report