MongoDB Advanced Query Details

Source: Internet
Author: User
Tags findone mongodb download

Preface

Before a few, old players bypass can, novice night idle is also egg pain, as the command knocked aside, so you will have a certain grasp of mongodb. If you have not installed MongoDB to see my previous blog MongoDB download installation and simple additions and deletions

Prelude: Start the MONGDB database service and enter the shell interface

> cmd

> CD C:\Program files\mongodb\bin--Enter the bin directory of the MONGDB installation file.

> net start MongoDB; --Open MongoDB database service

> Mongo--Enter the shell interface

Common Commands

> Show DBS--View a list of databases

> Use admin--create admin database with admin database if existing admin database

> DB---Displays the name of the database currently in use

> db.getname ()---Displays the name of the database currently in use

> Db.dropdatabase ()---delete the database currently in use

> Db.repairdatabase ()--Repair the current database

> db.version ()--Current database version

> Db.getmongo ()--View the link machine address of the current database

> db.stats () Displays the current database status, including database name, number of collections, current database size ...

> Db.getcollectionnames ()--see those collections in the Database (table)

> Db.person.drop ()--Delete the current collection (table) person

MongoDB access Javascrip style grammar, For,while,next,hasnext,foreach,toarray,findone,limit

Note:

1, P={name: "Zhang Long Hao", age:18} This is not the canonical JSON format, using this similar JavaScript syntax, the object will be automatically complement the specification of the JSON format {"name": "Zhang Long Hao", "Age": 18}.

2, I inserted here 2 data, one is the insert syntax, one is the save syntax, where the insert and save is the same function.

Note: Here I mainly use a for syntax, the loop inserted 4 data, is not a little excited. Haha, MongoDB is so wayward.

Note: Here's the query.

1, while: As a programmer should not be unfamiliar he is a cycle.

2, Hasnext:cursor collection traversal, whether there is data.

3. Printjson: The document in the output collection

4, Next: The current document, and traverse down.

Note:foreach loop input, a function must be defined for each cursor element to be called.

Note: Cursors can also be used as arrays.

Note: The cursor is converted to a real array type, using the.

Note:findone, returns the first data in the result set. Limit (3), which returns the first three data in the result set.

advanced queries in MongoDB

The important problem of the document-oriented NoSQL database is not the high-performance concurrent reading and writing problem, but the guarantee of the massive data storage, which has better query performance than the general database.

NOTE: Conditional action symbols: >, <, >=, <=

1, $GT//greater than >, $LT//less than <, $GTE//greater than or equal to >=, $lte//less than or equal to

2. {"filed": {$op, value}}//filed field, $op conditional action symbol, value.

3. {"Age": {$gt: 1}}: All data documents older than 1 in the person collection

Note: The $all matches all, like in T-SQL, but the in of T-SQL satisfies any one of the parentheses, and the $all in MongoDB must satisfy all the values in [].

1, {age:{$all: [7,9]}}:age array, as long as there are 7 and 9 satisfies the condition. If only 7, No 9 is not eligible.

Note: $exists determine if a field exists, (True/false)

1. {city:{$exists: true}}: Data for the City field exists in the collection

Note: $mod modulo operation.

1. {age:{$mod: [7,6]}}: Set of 7 + 6 data

Note: $ne not equal to

Note: $in included, $nin not included. Same as In,not in T-SQL.

1. {age:{$in: [10,11]}}: If age is an array, it can be retrieved whenever the array contains any of the data in the in condition. is not an array, it can be retrieved as long as any one of the conditional data in is satisfied.

Note: The number of array elements.

1. {age:{$size: 4}}:age data result set with 4 array elements.

Note: $not regular match, does not contain data that begins with a sheet.

Note:skip,limit,sort,count

1. Skip (2): Start query from the second section of the dataset

2, Limit (2): 2 data is detected in turn.

3, sort ({age:1}): 1. Positive order query, 1 flashback query.

4. Count (): Total number of result sets.

MongoDB Advanced Query Details

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.