Get the App
SLTechnology News&Howtos  ›  Database  › 

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

Shulou Source: shulou.com Published: 2022-06-01 02:39:29 10月03日 Update

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.

Tags: Data database query example document help no parameter instance version program program design expression reference growth update design Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Linux Microsoft Docker