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

An example of CURD simple operation for getting started with mongodb database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

In this paper, an example is given to illustrate the simple operation of CURD for getting started with mongodb database. Share with you for your reference, the details are as follows:

View all databases

Some versions of show dbs;show databases; # may not work

Using the database

Use database name

View the collection (the collection is the table of the mysql)

Show tables/collections

View help

Db.help ()

Create a library

The library of MongoDB is created implicitly and can use a library that does not exist.

Then you can create a library by creating a colletion under the library.

Delete database

Db.dropDatabase ()

Create a collection

Db.createCollection (name, options)

Example: > db.createCollection ('student')

Delete a collection

Db.collectionName.drop ()

Insert document (add data)

Db.COLLECTION_NAME.insert (document)

Example:

Db.student.insert ({name:'zhang san',age:10})

Delete document

Db.COLLECTION_NAME.remove (query expressions, options)

Example:

Db.student.remove ({'_ id':ObjectId ('5bc57f9e0a64e83e9db621ca')})

The second parameter:

JustOne: (optional) if set to true or 1, only one document is deleted, which is equivalent to limit 1.

Update document

Db.collectionName.update (, {upsert:, / / if not, add data multi:, writeConcern:})

Example:

Db.student.update ({'age':12}, {$set: {' name':'li si2'} $rename: {'birth':'birthday'} $inc: {' age':16}, $unset: {'status':1}})

Description:

If you do not use $set, the whole piece of data will be modified to new data.

After using $set, modify only the columns you want to modify.

$unset deletes a column $rename renames a column $inc increases the value of a column

Query document

Db.collection.find (query, projection)

Example:

Db.student.findOne (); / query a db.student.find (); / query all db.student.find ({}, {name:1}); / / query name column (including _ id) db.student.find ({}, {name:1,_id:0}); / / query name column (excluding _ id)

It is hoped that what is described in this article will be helpful to the programming of MongoDB database.

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