1. 程式人生 > >重新學習mongodb:構建查詢

重新學習mongodb:構建查詢

findone和find:

find返回一個游標。findone返回一個檔案;

忽略、限制和排序查詢選項:

db.reviews.find().skip().limit(12);

加入排序功能:

db.reviews.find().sort({'_helpful_votes':-1}).limit(12)

不返回整個檔案,使用投影返回限制欄位:

db.users.findOne({username: 'kbanker',
  hashed_password: 'bd1cfa194c3a603e7186780824b04419'},
  {_id: 1})

所有:

// Product page
db.products.findOne({'_id': ObjectId("4c4b1476238d3b4dd5003981")})

// Displaying a product page.
//<start id="product-page"/>  
product  = db.products.findOne({'slug': 'wheel-barrow-9092'})
category = db.categories.findOne({'_id': product['main_cat_id']})
reviews  = db.reviews.find({'product_id': product['_id']})
//<end id="product-page"/>  

//<start id="product-page-find-one"/>  
db.products.find({'slug': 'wheel-barrow-9092'}).limit(1)
//<end id="product-page-find-one"/>  

//<start id="product-page-review-skip-limit"/>  
reviews = db.reviews.find({'product_id': product['_id']}).skip(0).limit(12)
//<end id="product-page-review-skip-limit"/>  

//<start id="product-page-review-skip-limit-sort"/>  
reviews = db.reviews.find({'product_id': product['_id']}).
                           skip(0).limit(12).sort({helpful_votes: -1})
//<end id="product-page-review-skip-limit-sort"/>  


//<start id="product-page-final"/>  
var page_number = 10
product  = db.products.findOne({'slug': 'wheel-barrow-9092'})
category = db.categories.findOne({'_id': product['main_cat_id']})
reviews_count = db.reviews.count({'product_id': product['_id']})
reviews = db.reviews.find({'product_id': product['_id']}).
                         skip((page_number - 1) * 12).
                         limit(12).
                         sort({'helpful_votes': -1})
//<end id="product-page-final"/>  

//<start id="product-listing-page"/>  
category = db.categories.findOne({'slug': 'outdoors'})
siblings = db.categories.find({'parent_id': category['_id']})
products = db.products.find({'category_id': category['_id']}).
                           skip((page_number - 1) * 12).
                           limit(12).
                           sort({helpful_votes: -1})
//<end id="product-listing-page"/>  

// Displaying the category hierarchy
//<start id="category-page-root"/>  
categories = db.categories.find({'parent_id': null})
//<end id="category-page-root"/>  

mongodb支援使用正則表示式查詢:

db.users.find({'last_name':/^Ba/});

查詢特定的範圍:

db.users.find({'addresses.zip':{'$gt':10019,'$lt':10040}})


db.users.findOne({username: 'kbanker',
  hashed_password: 'bd1cfa194c3a603e7186780824b04419'})

db.users.findOne({username: 'kbanker',
  hashed_password: 'bd1cfa194c3a603e7186780824b04419'},
  {_id: 1})

db.users.find({last_name: 'Banker'})

db.users.find({last_name: /^Ba/})

db.users.find({'addresses.zip': {$gte: 10019, $lt: 10040}})

db.orders.find({'line_items.sku': "9092"}) 

db.orders.find({'line_items.sku': "9092",
    'purchase_date': {$gte: new Date(2009, 1, 1)}})

db.orders.ensureIndex({'line_items.sku': 1, 'purchase_date': 1})

user_ids = db.orders.find({'line_items.sku': "9092",
  purchase_date: {'$gt': new Date(2009, 0, 1)}},
  {user_id: 1, _id: 0})
users = db.users.find({_id: {$in: user_ids}})

查詢選擇:

設定運算

布林運算子:

查詢關鍵字文件:

db.products.find({'details.color':{$exists:false}})//沒有這個欄位的資料

匹配子文件

關鍵字的順序非常重要。

陣列:

JavaScript查詢運算子:

 

  正則表示式:

查詢選擇:

對映:

跳過和限制:

skip , limit