The query in MongoDB

Source: Internet
Author: User
Tags findone

MongoDB Chinese Document: http://docs.mongoing.com/manual-zh/contents.html

Here, for example, with the collection name test, the database inserts some test data through a for loop, with the keys: name,age,date

  db.test.find () querying all the data

  Db.test.findOne () queries a single piece of data, returning the first inserted data, which can no longer be followed by other query conditions, such as: Db.test.findOne (). Sort ({age:1}), reported: Has no Method ' Sort '

  db.test.find ({age:18}) query for data aged 18

  db.test.find ({age:18}) [0] Queries age 18, the index in the resulting document is 0, andMongoDB's query returns the document documents (except the aggregate function ...), so you can take

  db.test.find (). Skip (1). Srot ({age:1}) is ordered in the positive order of age (1 is positive, 1 is reverse ), and the result set skips the first data (Skip ( 1)) The first 10 data

  db.test.find ({},{name:1,age:1,date:-1}) queries all data, and the columns in the result set contain only name, age, and not: date. The first bracket is the condition of the query, and the null representation queries all non-_id fields: 1 is displayed, 1 is hidden; _id field: Not specified as display, 0 is hidden

  Db.test.find ({age:{$gt:)) queries all information older than 18,$GT is greater than,$lt is less than

  Db.test.find ({age:{$in: [17,18,19]}}) queries age=17, 18, 19 data, with SQL in

Insert two data for continuation, multiple sex keys:

Db.test.insert ({name: "Testss", age:20,sex:1})

Db.test.insert ({name: "Tdfdaf", age:20,sex:0})

db.test.find ({age:{$lt: 21},sex:1}) query for data that is less than 21 and has a gender of 1, which is SQL's and query

  Db.test.find ({$or: [{age:20},{sex:1}]}) query for data with age 20, or 1 for gender, that is, SQL or query

  Db.test.find ({age:{$lt: A, $or: [{age:20},{sex:1}]}) used with and and or, multi-analysis analyst Three statement, writing format

  

The query in 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.