Basic operation of MongoDB

Source: Internet
Author: User
Tags logical operators

Basic operation of MongoDB

Enter MONGO into the database in CMD. Enter show databases can query the existing database, admin and local are the own database, not

To operate these two databases. The following is the basic operation of MongoDB: additions and deletions to check and change!

Before we do the data processing, we need to build a collection of libraries:

  Build Library : Use the command: using+ library name , such as: Useddan ; if the library name exists then go to the library and create it if it doesn't exist! Rollback occurs if no data is added

Phenomenon, using show DBS to view the database, will show no this library!

Added data   :db.test.insert ({' name ': ' Nike ', ' sex ': ' Man '}) add the ID by default. Notice here that the data passed in is in the form of a dictionary! Test as Set

Together, use show tables to view all the collections of the current library.

    

       db.test.save ({' name ': ' DC&CN ', ' sex ': ' * '}) the Save method can also insert data, and you can modify the data if you specify an existing ID.

To facilitate the following elaboration, we add a few more data!

  query data :db.test.find () queries the data in the test collection, displaying 20 by default.

        Db.test.find ({' age ': {$eq: ' + '}}) queries the data in the test collection for age equal to ' 25 '. If the data is inserted when the number is a character

String passed in, you must use quotation marks here, or not match!

    

Here the last piece of data, 25 is directly inserted in the value, the query can not be found, so the format of the data must pay attention to!

        Db.test.find ({' age ': {$eq: ' + '}, ' sex ': ' Man '}) queries the data in the test collection for age equal to ' 25 ' and sex for males. This also shows the

Multi-conditional filtering.

  Delete data : Db.test.drop ({' Age ': ' 25 '})

        Db.test.remove ({' Age ': ' +} ') delete the Age field value of ' 25 ' in the test collection

        db.test.remove ({' name ': ' Nike ', ' Age ': '} ') multi-Conditional match Delete, note that remove matches several data to delete several.

db.test.remove ({}) deletes the current document, but the collection is still in, an empty collection

db.test.drop () Delete collection

  Update data :db.test.update ({' name ': ' DC&CN '},{$set: {' age ': +}})

Here, the data named DC&CN is changed to 23, and No field is added. Each time it is modified to match to the first one. The multi parameter is set to True to

Update all the data. such as:db.test.update ({' name ': ' Lining '},{$set: {' age ': 2000}},{' multi ': 3}) here the 3 defaults to True,

The data that meets the requirements will be updated all.

The MongoDB operator

Comparison operators

  $ eq equals $ GT greater than $ GTE not less than $ lt less than

  $ LTE not less than $ ne is not equal to $ in an array that represents a match in an array element

  logical operators

OR and not nor

  Db.test.find ({$or: [{' Age ': 25},{' sex ': ' Man '}]}) matches data with age 25 or sex as man. $or is followed by an array.

  Db.test.find ({$and: [{' Age ': 25},{' sex ': ' Man '}]}) matches two conditions that satisfy the same data, equivalent to:

                        Db.test.find ({' Age ': ' + ', ' sex ': ' Man '})

   Db.test.find ({' age ': {$not: {$eq: ' +}} ') matches age not equal to 25. $not operator only affects other operators that cannot check fields independently

and documentation. There is no difference between using not and comparing operators, but there is a clear difference between changing $eq to $gt . As shown in the following:

  

  Db.test.find ({$nor: [{' Age ': ' + '}]}) if the elements inside the array use the comparison operator, the match fails

  Update operator :

$inc Plus and minus

  $mul Multiplication

  $rename Renaming

  $set Update One field, other reserved

Basic operation of MongoDB

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.