MongoDB basic Commands

Source: Internet
Author: User
Tags mongodb tutorial php tutorial

Description: mongodb[1] is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for Web applications.

Mongodb

MONGODB[2] is a product between a relational database and a non-relational database, the most versatile of the non-relational databases, most like relational databases. The data structure he supports is very loose and is a JSON-like Bson format, so you can store more complex data types. MONGO's biggest feature is that the query language he supports is very powerful, and its syntax is a bit like an object-oriented query language that almost implements most of the functionality of a relational database single-table query, and also supports indexing of data.

Distributed File System (distributed) means that the physical storage resources managed by the file system are not necessarily directly connected to the local nodes, but are connected to the nodes through the computer network. The design of a distributed file system is based on client/server mode. A typical network might include multiple servers for multiple users to access. In addition, the peer-to feature allows some systems to play the dual role of the client and server.

HBase is a distributed, column-oriented, open-source database that comes from the Google paper "Bigtable: A distributed storage system of structured data" written by Fay Chang.

The Yonghong Data mart is a software that is based on proprietary technology developed for the storage and processing of information. Yonghong Data Mart's Distributed File Storage System (ZDFS) is a transformation and extension of Hadoop HDFs to manage and store files stored on all nodes in a server cluster.

The above is a brief introduction to MongoDB, by introducing the use of MongoDB in addition to understanding his basic data storage, but also to understand how distributed systems are deployed, and how to solve load balancing problems.

The following is a summary of my beginner's tutorial, which is familiar with basic commands, understanding MongoDB features, and so on.

A Basic use in command mode

    1. Database creation

Use database_name

    1. View Database

Show Dbsshow DBS

    1. Deleting a database

Db.dropdatabase ()

    1. Insert Document

Db.user.insert ({_id:1,name: ' Mybo '})

INSERT statement using JSON-like Bson

    1. Updating a document that already exists

Db.collection.update (

<query>,

<update>,

{

Upsert: <boolean>

Multi: <boolean>

Writeconcern: <document>

}

)

Parameter description:

    • The query:update query condition, similar to that in SQL Update query, is behind.
    • Update:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • 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.

Exmple:db.col.update ({' title ': ' MongoDB Tutorial '},{$set: {' title ': ' MongoDB '}},{multi:true})

    1. Replace document

Db.col.save ({

"_id": ObjectId ("56064f89ade2f21f36b03136"),

"title": "MongoDB",

"description": "MongoDB is a Nosql database",

"By": "Runoob",

"url": "Http://www.runoob.com",

"Tags": [

"MongoDB",

"NoSQL"

],

"Likes": 110

})

    1. Delete a document

Db.col.remove ({' title ': ' MongoDB Tutorial '})

Delete the first: db. Collection_name.remove (deletion_criteria,1)

    1. Delete all Documents
    2. Db.col.remove ({})
    3. Document Query

Db.col.find ()//query All

    1. Conditional query Examples

Db.col.find ({"likes": {$gt: $}, $or: [{"By": "Rookie Tutorial"},{"title": "MongoDB Tutorial"}]}). Pretty ()

    1. Conditional operator
    • (>) Greater than-$GT
    • (<) less than-$lt
    • (>=) greater than or equal to-$gte
    • (<=) less than or equal to-$lte
    1. Type operation

The $type operator is based on the Bson type to retrieve the matching data type in the collection and returns the result.

    1. Limit limits two data

Db.col.find ({},{"title": 1,_id:0}). Limit (2)

{"title": "PHP Tutorial"}

{"title": "Java Tutorial"}

    1. In addition to using the limit () method to read a specified amount of data, we can use the skip () method to skip a specified number of data, Skip The method also accepts a numeric parameter as the number of skipped record bars.

Db.col.find ({},{"title": 1,_id:0}). Limit (1). Skip (1)

{"title": "Java Tutorial"}

    1. Sort

Db. Collection_name.find (). Sort ({key:1})//1 Ascending 2 Descending

    1. Build an index

B.col.ensureindex ({"title": 1}).

18. Aggregation

MongoDB Aggregation (aggregate) is primarily used to process data (such as statistical averages, sums, etc.) and returns the computed data results. A bit like the count (*) in the SQL statement.

Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$sum: 1}}])

{

"Result": [

{

"_id": "w3cschool.cc",

"Num_tutorial": 2

},

{

"_id": "Neo4j",

"Num_tutorial": 1

}

],

"OK": 1

}

    1. Regular expressions

Db.posts.find ({post_text:{$regex: "w3cschool.cc", $options: "$i"}})

The above is a command-line operation for MongoDB, but for programmers it is easy for us to use some simple commands, but for as a Java developer I want to be proficient in understanding how to call in Java.

MongoDB basic Commands

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.