MongoDB Common Commands

Source: Internet
Author: User
Tags findone mongodb

Query whether a field exists

Db.student.findOne ({name:{$exists: true}})

Db.student.findOne ({' department.name ': {$exists: true}})

Db.student.findOne ({' department.name ': {$exists: false}})

-------------------------------------------------------------------------------

Determine if the Map object is empty, note that the size can not be judged by $GT, $LT, and the index is used to determine

Db.student.find ({class: {$not: {$size: 0}}}). Limit (2)

Db.student. Find ({' class.0 ': {$exists: 1}})

----------------------------------------------------------------------------------

Determine the size of an array

Db.student. Find ({' class ': {$size: 3}})

Db.student.find ({$where: "This.class.length < 3"})

---------------------------------------------------------------------------------

Formatted output

Select top 1 *from student;

Db.student.findOne ()

Select top 1 *from student where age>15;

Db.student.findOne ({age:{$gt: 15}})

Select Class,department from student where name= ' shell ' and gender=true;

Db.student.findOne ({name:shell,gender:true},{class:1,department:1})

---------------------------------------------------------------------------------

The number of cursor jump queries and qualified returns

Db.department.find (). Skip (10)

Select top Ten *from department;

Db.department.find (). Limit (10)

---------------------------------------------------------------------------------

The value returns the specified field, the first map writes the filter condition, the second map writes the returned field, and the default returns _ID, if it is not necessary to set the _id:0

Db.student.find ({},{name:1,id:1}

Db.student.find ({},{name:1,id:1,_id:0}

--------------------------------------------------------------------------------------

Total queries

Db.student.find (). Count ()

Db.student.find ({age:{$lt:}}). Count ()

-----------------------------------------------------------------------------------

Update and delete a field: Delete the first class and modify the update time to the current time

Db.student.update ({id:10005},{$unset: {' class.0 ': 1}, $set: {Updated:numberlong (New Date (). ValueOf ()/1000)}})

------------------------------------------------------------------------------------

MongoDB Common 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.