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

Operation method of MongoDB database document (must-see article)

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

Share

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

The previous words

This article will introduce the addition, deletion, modification and query of documents in MongoDB database in detail.

If no collection exists in the database, MongoDB creates the collection and then inserts the document into the collection

To insert multiple documents in a single query, pass an array of documents in the insert () command

You can insert multiple documents using js syntax

[save ()]

You can also use db.post.save (document) to insert a document. If you do not specify _ id in the document, the save () method automatically assigns the value of ID like the insert () method. If you specify _ id, all data of the document containing _ id is replaced with the save () method.

In other words, the difference between the save () method and the insert () method is that the save () method can be overridden or modified, while the insert () method cannot

Db.post.save (document)

[insertOne ()]

You can insert a single document into a collection using the db.collection.insertOne () method

[insertMany ()]

You can insert multiple documents into a collection using the db.collection.insertMany () method

Query document

[find ()]

To query data from the MongoDB collection, you need to use the find () method of MongoDB, which returns the first 20 documents in the result by default, and type "it" to display the next 20 documents.

The basic syntax of the find () command is as follows:

Db.COLLECTION_NAME.find (document)

The find () method will display all documents in an unstructured manner

Query conditions can be limited

You can specify the returned key through the second parameter of find. A value of 1 or true indicates that the key is displayed, and a value of 0 or false indicates that the key is not displayed.

The count () method under the find () method can display the number of documents that meet the criteria.

[findOne ()]

The findOne () method returns only one document, which is the first to be added

[comparison operator]

Less than {: {$lt:}} less than or equal to {: {$lte:}} greater than {: {$gt:}} greater than or equal to {: {$gte:}} is not equal to {: {$ne:}} equals {: {$eq:}}

Get a value of x less than 2

Get a value of x greater than or equal to 2

Get a value that x is not equal to 2

[logical operator]

You can use the logical operators $and, $or to represent and, or

{$and: [{}, {},..., {}]} {$nor: [{}, {},... {}]}

[regular expression]

Document queries can use regular expressions, but only support data of string type

[$where]

The $where operator is powerful and flexible. It can use any JavaScript as part of the query, including strings of JavaScript expressions or JavaScript functions.

Use Strings

Use function

Limit and skip

[limit ()]

If you need to read a specified number of data records in MongoDB, you can use the Limit method of MongoDB, and the limit () method accepts a numeric parameter that specifies the number of records to read from MongoDB

The first 20 documents in the result are returned by default, and type "it" to display the next 20 documents.

If no parameters in the limit () method are specified, all data in the collection is displayed

Db.COLLECTION_NAME.find () limit (NUMBER)

[skip ()]

You can use the skip () method to skip a specified amount of data, and the skip method also accepts a numeric parameter as the number of skipped records

Db.COLLECTION_NAME.find () skip (NUMBER)

Sort

[sort ()]

In MongoDB, the sort () method is used to sort the data, and the sort () method can specify the sorted field by parameter, and use 1 and-1 to specify the sort method, where 1 is ascending sort and-1 is used for descending sort.

Db.COLLECTION_NAME.find () .sort ({KEY:1})

Update document

MongoDB uses the update () or save () method to update documents in the collection

[update ()]

The update () method is used to update existing documents. The syntax format is as follows:

The query condition of db.collection.update (, {upsert:, multi:, writeConcern:}) query: update, similar to the object of update: update after where in sql update query and some updated operators (such as $, $inc...), etc. It can also be understood as upsert after set in sql update query: optional. This parameter means that if there is no record of update, whether to insert objNew,true is inserted. Default is false, and no multi is inserted: optional. Mongodb defaults to false. Only the first record found is updated. If this parameter is true, update writeConcern for multiple records found according to the condition: optional, the level of exception thrown

[note] after testing, the upsert parameter cannot be set to true or false, and new fields can be inserted.

By default, mongodb updates only the first record found, updating XRV 1 to XRV 10.

It is important to note that if you do not use $set, replace the contents of the document with xpur10

Update all records, update xPlus 10 to xpurl.

By default, mongodb is only added to the first record found by the update.

Add all the records of XRV 2 found, and add ZRO 2

[save ()]

The save () method can insert or update a document, if the _ id of the document in the parameter is different from the _ id that exists in the collection; if it is the same, update

Delete document

The MongoDB remove () function is used to remove data from the collection.

[remove ()]

By default, mongodb deletes all documents that meet the criteria

Db.collection.remove (, {justOne:, writeConcern:}) query: the condition of the deleted document. JustOne: (optional) if set to true or 1, only one document is deleted. WriteConcern: (optional) the level at which the exception is thrown.

Delete only the first document that meets the criteria

Delete all documents that meet the criteria

Insert document

To insert data into the MongoDB collection, you need to use the insert () or save () method of MongoDB, as well as the insertOne () or insertMany () method

[insert ()]

The basic syntax of the insert () command is as follows

Db.COLLECTION_NAME.insert (document)

In an inserted document, if you do not specify the _ id parameter, MongoDB assigns a unique ObjectId to the document. _ id is a 12-byte hexadecimal number unique to each document in the collection

The above MongoDB database document operation method (must see article) is the editor to share with you all the content, I hope to give you a reference, but also hope that you support more.

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