MongoDB Database and MySQL operation

Source: Internet
Author: User
Tags mongodb query

On the left is the MONGODB query statement, and the right is the SQL statement. Compared with the use, very convenient.

Db.users.find () SELECT * from Users

Db.users.find ({"Age": +}) SELECT * from the users where age = 27

Db.users.find ({"username": "Joe", "Age": +}) SELECT * from the users where "username" = "Joe" and age = 27

Db.users.find ({}, {"username": 1, "email": 1}) Select Username, email from users

Db.users.find ({}, {"username": 1, "_id": 0})//No case// Instant column filter is added,_id will also return; must explicitly block _id return

Db.users.find ({"Age": {"$gte": +, "$lte": ()}) select * from the users where age >=18 and age <=//$lt (<) $l Te (<=) $gt (>) $gte (>=)

Db.users.find ({"username": {"$ne": "Joe"}}) select * from users where username <> "Joe"

Db.users.find ({"Ticket_no": {"$in": [725, 542, 390]}) SELECT * from the users where Ticket_no in (725, 542, 390)

Db.users.find ({"Ticket_no": {"$nin": [725, 542, 390]}) SELECT * from the users where ticket_no not in (725, 542, 390)

Db.users.find ({"$or": [{"Ticket_no": 725}, {"Winner": True}]}) select * Form users where Ticket_no = 725 or winner = TR Ue

Db.users.find ({"Id_num": {"$mod": [5, 1]}}) select * from Users where (id_num mod 5) = 1

Db.users.find ({"$not": {"Age": +}}) select * from the users where not (age = 27)

Db.users.find ({"username": {"$in": [null], "$exists": True}}) select * from the users where username is null// if directly through the /c0>Find ({"username": null}) for query, then the " no username" record is filtered out

Db.users.find ({"Name":/joey?/i})// regular query,value is an expression that conforms to PCRE

Db.food.find ({fruit: {$all: ["Apple", "Banana"]}})// array of Queries , field fruit , contains both "Apple" with a record of "Banana"

db.food.find ({"fruit.2"  :  "Peach"})//  query for array Span style= "Font-family:times New Roman" >,&NBSP; field fruit 3 from 0 Start element is peach

db.food.find ({"Fruit"  : {"$size"  : 3}})//  Query for arrays ,&NBSP; query array element number is 3 Records, $size Front cannot be combined with other operators

Db.users.findOne (criteria, {"Comments": {"$slice":})//array of queries, returning only the first 10 of the arrays comments , can also {"$slice": -10}, {"$slice": [Max]}; return the last Ten Bar, and the middle Ten Strip

Db.people.find ({"Name.first": "Joe", "Name.last": "Schmoe"})// nested query

Db.blog.find ({"Comments": {"$elemMatch": {"author": "Joe", "score": {"$gte": 5}}})// nested query only when nested elements are arrays using ,

Db.foo.find ({"$where": "this.x + This.y = ="})// complex queries,$where of course are very convenient, but inefficient. For complex queries, the order of consideration should be regular---MapReduce-- $where

Db.foo.find ({"$where": "function () {return this.x + This.y = = Ten;}"})//$where can support JavaScript functions as Query Criteria

Db.foo.find (). Sort ({"X": 1}). Limit (1). Skip (10); return to paragraph (ten, one), by "x" to sort ; three x Limit The order is arbitrary, should try to avoid Skip used in Large-number

MongoDB Database and MySQL operation

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.