MongoDB: Queries and aggregation 1

db.products.findOne({'slug': 'wheel-barrow-9092'})
db.categories.findOne({'_id': product['main_cat_id']})

<=>

db.products.find({'slug': 'wheel-barrow-9092'}).limit(1)


db.reviews.find({'product_id': product['_id']})

db.reviews.find({'product_id': product['_id']}).skip(0).limit(12)

db.reviews.find({'product_id': product['id']}).sort({helpful_votes: -1}).limit(12)

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})

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",'purchase_date': {$gte: new Date(2009, 0, 1)}})

user_ids = db.orders.find({'line_items.sku': "9092",purchase_date: {'$gt': new Date(2009, 0, 1)}},
                                                {user_id: 1, _id: 0}).toArray().map(function(doc) { return doc['_id'] })

users = db.users.find({_id: {$in: user_ids}})

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

db.users.find({first_name: "Smith", age: 40})

db.users.find({age: {$gte: 0, $lte: 30})

db.products.find({main_cat_id: { $in:
           [ObjectId("6a5b1476238d3b4dd5000048"),
            ObjectId("6a5b1476238d3b4dd5000051"),
            ObjectId("6a5b1476238d3b4dd5000057") ] } } )

db.products.find('details.color': { $nin: ["black", "blue"] }

db.products.find(tags: { $all: ["gift", "garden"] }

BOOLEAN OPERATORS($ne, $not, $or, $and, and $exists.)

db.products.find('details.manufacturer': 'ACME', tags: {$ne: "gardening"} }

db.products.find({ $or: [{'details.color': 'blue'}, {'details.manufacturer':'ACME'}] })

db.products.find({$and: [
                             {tags: {$in: ['gift', 'holiday']}},
                             {tags: {$in: ['gardening', 'landscaping']}}
                          ]})

db.products.find({'details.color': {$exists: true}})

<=>

db.products.find({'details.color': {$ne: null}})

db.products.find({'details.manufacturer_id': 432});

{ _id: {sym: 'GOOG', date: 20101005}
open: 40.23,
high: 45.50,
low:
38.81,
close: 41.22
}

db.ticks.find({_id: {sym: 'GOOG', date: 20101005} });     //vv

db.ticks.find({_id: {date: 20101005, sym: 'GOOG'} });     //xx

{ _id: ObjectId("4c4b1476238d3b4dd5003981"),
slug: "wheel-barrow-9092",
sku: "9092",
tags: ["tools", "equipment", "soil"]

}

db.products.find({tags: "soil"})

db.products.find({'tags.0': "soil"})

{ _id:
ObjectId("4c4b1476238d3b4dd5000001")
username: "kbanker",
addresses: [
        {name:"home",
         street: "588 5th Street",
         city:"Brooklyn",
         state:"NY",
         zip:11215},
      {name:"work",

        street:"1 E. 23rd Street",
        city:"New York",
        state:"NY",
        zip:10010}
     ]
}

db.users.find({'addresses.0.state': "NY"})

db.users.find({addresses: {$elemMatch: {name: 'home', state: 'NY'}}})

db.users.find({addresses: {$size: 3}})

JAVASCRIPT

If you can’t express your query with the tools described thus far, then you may need to write some JavaScript. You can use the special $where operator to pass a JavaScript expression to any query.

db.reviews.find({$where: "function() { return this.helpful_votes > 3; }"}})

db.reviews.find({$where: "this.helpful_votes > 3"}})

REGULAR EXPRESSIONS

db.reviews.find({user_id: ObjectId("4c4b1476238d3b4dd5000001"),text: /best|worst/i })

MISCELLANEOUS QUERY OPERATORS

db.orders.find({subtotal: {$mod: [3, 0]}})

db.users.find({_id: {$type: 2}})

PROJECTIONS

db.users.find({}, {username: 1})

db.users.find({}, {addresses: 0, payment_methods: 0})

db.products.find({}, {reviews: {$slice: 12}})
db.products.find({}, {reviews: {$slice: -5}})

db.products.find({}, {reviews: {$slice: [24, 12]}})

db.products.find({}, {reviews: {$slice: [24, 12]}, 'reviews.rating': 1})

SORTING

db.reviews.find({}).sort({rating: -1})

db.reviews.find({}).sort({helpful_votes:-1, rating: -1})

SKIP AND LIMIT

db.docs.find({}).skip(500000).limit(10).sort({date: -1})    //inefficient

db.docs.find({date: {$gt: previous_page_date}}).limit(10).sort({date: -1})  //efficient

猜你喜欢

转载自ylzhj02.iteye.com/blog/2077928