MongoDB introduction and the increase and deletion check

Source: Internet
Author: User
Tags data structures mongodb

First, Introduction

MongoDB is written by the C + + language and is an open source database system based on distributed file storage. MongoDB is designed to provide scalable, high-performance data storage solutions for Web applications. MongoDB stores data as a document and data structures consist of key-value (key=>value) pairs. A MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and array of documents.

MongoDB Data type:

Data Type Description
String String. Data types commonly used to store data. In MongoDB, the UTF-8 encoded string is legal.
Integer Integer value. Used to store numeric values. Depending on the server you are using, it can be divided into 32-bit or 64-bit.
Boolean Boolean value. Used to store Boolean values (True/false).
Double Double-precision floating-point value. Used to store floating-point values.
Min/max keys Compares a value to the lowest and highest value of a BSON (binary JSON) element.
Arrays Used to store an array or list or multiple values as a single key.
Timestamp Time stamp. Record when the document was modified or added.
Object Used for inline documents.
Null Used to create a null value.
Symbol Symbol. The data type is basically the same as the string type, but unlike it, it is typically used in languages with special symbol types.
Date Date time. Use the UNIX time format to store the current date or time. You can specify your own datetime: Create a Date object and pass in the month-date information.
Object ID The object ID. The ID used to create the document.
Binary Data Binary data. Used to store binary data.
Code The code type. Used to store JavaScript code in a document.
Regular expression The regular expression type. Used to store regular expressions.

Second, the operation

1. Database Operation instructions

(1) Create a database

Command: Use dbname, example: Use test if the database test exists, select the test database, and if it does not exist, create the database test.

(2) View all databases

Command: Show DBS

(3) Deleting a database

Command: Db.dropdatabase () to delete the current database

2. Document Operation instructions

The document in MongoDB is equivalent to row in a relational database, and the document's data structure is basically the same as JSON Bson.

(1) Inserting a document

MongoDB uses the Insert () or Save () method to insert a document into the collection with the following syntax: DB. Collection_name.insert (document).

Example: Db.mycol.insert ({name: ' Test1 ', age:20,sex: ' Male '), where MyCol is the collection name, and if the collection does not exist, MongoDB is created automatically first.

You can also use DB. Collection_name.save inserts a document, similar to insert, and updates the _id document if the _id field is specified.

(2) Update document

MongoDB updates the document with update or save, and the update syntax:

Db.collection.update (
<query>,
<update>,
{
Upsert: <boolean>
Multi: <boolean>
Writeconcern: <document>
}
)

Query:update the query conditions;

Update:update objects and some updated operators (such as $, $inc ... ) and so on, can also be understood as SQL update query within the set after;

Upsert: Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.

Multi: Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.

Writeconcern: Optional, throws an exception level.

Example: First insert a document: Db.mycol.insert ({name: ' Test1 ', age:1,sex: ' Male '), and then perform the update operation: db.mycol.update ({' name ': ' Test1 '},{$set: { ' Sex ': ' Formale '}) The results are as follows:

Set Multi to True if you need to modify more than one document that meets the criteria. Example: Db.mycol.update ({' name ': ' Test1 '},{$set: {' sex ': ' Formale '}},{multi:true})

Save method: Replaces an existing document with an incoming document. The syntax format is as follows:

Db.collection.save (
<document>,
{
Writeconcern: <document>
}
)

Document: Documentation data, Writeconcern: the level at which an exception is thrown.

(3) Deleting a document

MongoDB uses Db.col.remove () to delete a document with the following syntax (before 2.6):

Db.collection.remove (
<query>,
<justOne>
)

After version 2.6:

Db.collection.remove (
<query>,
{
Justone: <boolean>
Writeconcern: <document>
}
)

Query (optional): Deleted condition, justone (optional): If set to TRUE or 1 deletes only one, Writeconcert (optional): the level at which the exception is thrown.

Example: Db.mycol.remove ({' name ': ' Test1 '})

(3) Querying documents

Db. The Collection_name.find () method displays all data in a unstructured manner, db. The Collection_name.find (). Pretty () method displays all data in a formatted manner. In addition to the FindOne () method, only one document is displayed.

MongoDB vs. relational database where comparison:

Operation format Example similar statements in an RDBMS
Equals {<key>:<value>} db.col.find({"by":"菜鸟教程"}).pretty() where by = ‘菜鸟教程‘
Less than {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty() where likes < 50
Less than or equal to {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater than {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater than or equal to {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not equal to {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty() where likes != 50

MongoDB and Condition: MongoDB's Find () method can pass in multiple keys (key), each with a comma, separated by a syntax format as follows:

Db.col.find ({key1:value1, key2:value2}). Pretty ()

Example: Db.mycol.find ({' name ': ' tes1 ', ' sex ': ' Formale '}). Pretty (), the effect of this sentence is similar to Sql:where name= ' test1 ' and sex= ' Formale ',

MongoDB or condition: the MONGODB or conditional statement uses the keyword $or, and the syntax format is as follows:

Db.col.find (
{
$or: [
{key1:value1}, {key2:value2}
]
}
). Pretty ()

Originally from: https://www.cnblogs.com/onephp/p/6252472.html

MongoDB introduction and the increase and deletion check

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.