Mongodb5--mongodb additions and deletions to search

Source: Internet
Author: User
Tags add time

Before we discussed the MongoDB "adding and deleting", to know that our additions and deletions are inseparable from the query expression, so the query expression in MongoDB is very important. About the query actually we just introduced to a small part.
We want to take a deep look at the query expressions for MongoDB.

We have a data set that is the product information of the mall:

Download Link: http://download.csdn.net/detail/u013517797/9726120

We want to insert the product information of the mall into MongoDB, the product has the following fields:
GOODS_ID Commodity PRIMARY Key
cat_id Area
Goods_name Product Name
Goods_number quantity of goods remaining in stock
Click_count Click Volume
Shop_price Hotel Prices
Add_time Add Time


We create a Marketplace database (shop) and then create a product table (goods) that sets the dataset
All of the data in the
(The data volume is larger when the console can not handle, so we put the data into three times into!) ):


Altogether there are IDs to 32 data, a total of 31 data. Use Db.goods.fond (); we can find 31 data to show that we have all the data inserted:

Db.goods.find (). count (); Instruction statistics the number of isolated data.

The next step is to learn more about query expressions.
Note: The following query is based on the imported product table.

1. Basic query where practice

Identify items that meet the following criteria
1.1: Commodity with primary key 32
Db.goods.find ({goods_id:32});


1.2: All items not in the 3rd column ($NQ)
$ne is the meaning of "! =" (Not equal to notequal)
Syntax: {field:{$ne: value}}
Function: Check the value of filed column is not equal to the document of value
Db.goods.find ({cat_id:{$ne: 3}},{cat_id:1,goods_name:1,_id:0});


1.3: The price of our shop is higher than 3000 yuan goods ($GT)
$GT is the meaning of ">" (Greater than great)
Syntax: {filed:{$gt: value}}
Function: Check the filed column for a document with a value greater than value
Db.goods.find ({shop_price:{$gt: 3000}},{goods_name:1,shop_price:1,_id:0});


1.4: The price of our store is less than 100 yuan of goods ($LT)
$lt is the meaning of "<" (less than Little)
Syntax: {filed:{$lt: value}}
Function: Check filed column value is less than equal to value of the document (equal to after the "E" can be, greater than the general)
Db.goods.find ({shop_price:{$lte: 100}},{goods_name:1,shop_price:1,_id:0});


1.5: Take out the 4th column or the 11th item ($in)
$in is the meaning of "in" (located)
Syntax: {filed:{$in: [Array contents]}}
Function: Check the value of the filed column in the document in the array []
Db.goods.find ({cat_id:{$in: [4,11]}},{cat_id:1,goods_name:1,_id:0});


1.5.2: Take out items that are not in the 4th or 11th column ($nin)
$nin is the meaning of "notin" (not located)
Syntax: {filed:{$nin: [Array contents]}}
Function: Check the filed column's value is not in the array [] Document
Db.goods.find ({cat_id:{$nin: [4,11]}},{cat_id:1,goods_name:1,_id:0});


1.6: Remove 100<= price <=500 ($and)
$and is the meaning of "and" (and)
Syntax: {$and: [{filed:value1},{filed:value2} ...]}
function: Check the document between Value1 and value2 in filed
Db.goods.find ({$and: [{shop_price:{$gte: 100}},{shop_price:{$lte: 500}}]},{shop_price:1,goods_name:1,_id:0});


1.7: Take out items that do not belong to the 3rd column and do not belong to the 11th column ($and $nin and $nor respectively)
$nor is the meaning of "neither nor" (neither and not)
Syntax: {$nor: [{filed:value1},{filed:value2} ...]}
Role: Check filed neither in value1 nor value2 documents
Db.goods.find ({$nor: [{cat_id:3},{cat_id:11}]},{goods_name:1,cat_id:1,_id:0});

Db.goods.find ({cat_id:{$nin: [3,11]}},{goods_name:1,cat_id:1,_id:0});

Db.goods.find ({$and: [{cat_id:{$ne: 3}},{cat_id:{$ne: 11}}]},{goods_name:1,cat_id:1,_id:0});


1.8: Take out goods with a price greater than 100 and less than 300, or greater than 3000 and less than 5000 ($or)

Db.goods.find ({$or: [{$and: [{shop_price:{$gt: 100}},{shop_price:{$lt: 300}}]},{$and: [{shop_price:{$gt: 3000}},{ shop_price:{$lt: 5000}}]}]},{shop_price:1,goods_name:1,_id:0});

Too long not very good to understand, we split up a bit:
Original statement:
{$or: [condition 1, Condition 2]},{shop_price:1,goods_name:1,_id:0}

The conditions 1 and 2 are the following two:
{$and: [{shop_price:{$gt: 100}},{shop_price:{$lt: 300}]}

{$and: [{shop_price:{$gt: 3000}},{shop_price:{$lt: 5000}]}


1.9: Remove the Goods_id%5==1 (5 is equal to 1), that is, 1,6,11,... Such goods ($MOD)
$mod is the meaning of "mod" (Take-over)
Syntax: {filed:{$mod: [Num1,num2]}}
Function: Check filed to NUM1 after the result is num2 all data
Db.goods.find ({goods_id:{$mod: [5,1]}},{goods_id:1,goods_name:1,_id:0});


1.10: Remove document with Add_time attribute ($exists)
$exists is the meaning of "exists" (existence)
Syntax: {filed:{$exists: 1}}
Function: Documents containing filed attributes will be found
Db.goods.find ({add_time:{$exists: 1}},{goods_name:1,add_time:1,_id:0});


1.11: Take out a document with a property of type string ($type)
$type is the meaning of "type"
Syntax: {filed:{$type: 1}}
Effect: A document with a type of filed property will be found
Type types are described by numbers:

Db.goods.find (goods_name:{$type: 2},{goods_id:1,goods_name:1,_id:0});


1.12: Take out student in the Hobby hobby array containing ' B ', ' C ' documents ($all)
$all is the meaning of "all" (inclusive)
Syntax: {filed:{$all: [Data 1, Data 2, Data 3 ...]}
Function: The Filed property (typically an array) contains data 1/2/3. doc will be found
Db.student.find ({hobby:{$all: [' B ', ' C ']}});

Mongodb5--mongodb additions and deletions to search

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.