Skip to content

Instantly share code, notes, and snippets.

@huanglong-zz
Last active January 1, 2016 00:19
Show Gist options
  • Save huanglong-zz/8066269 to your computer and use it in GitHub Desktop.
Save huanglong-zz/8066269 to your computer and use it in GitHub Desktop.
// drop indexes
db.accounts.dropIndex( { "tax-id": 1 } )
db.collection.dropIndexes()
// how to design schema and populate
var PersonSchema = new Schema({
name : String,
age : Number,
stories : [{ type: Schema.ObjectId, ref: 'Story' }]
})
var StorySchema = new Schema({
_creator : { type: Schema.ObjectId, ref: 'Person' },
title : String,
fans : [{ type: Schema.ObjectId, ref: 'Person' }]
})
var Story = mongoose.model('Story', StorySchema)
var Person = mongoose.model('Person', PersonSchema)
var aaron = new Person({name: 'Aaron', age: 100})
aaron.save(function (err) {
if (err) throw err
var story1 = new Story({
title: 'A man who cooked Nintendo',
_creator: aaron._id
})
story1.save(function (err) {
if (err) throw err;
aaron.stories.push(story1._id)
aaron.save(function (err) {
if (err) throw err;
Person
.findOne({name: 'Aaron'})
.populate('stories')
.run(function (err, person) {
if (err) throw err
console.log('person =', person)
console.log('person.stories =', person.stories[0])
})
Story
.findOne({title: /Nintendo/i})
.populate('_creator')
.run(function (err, story) {
if (err) throw err
console.log('story =', story)
})
})
})
})
// person = {
// stories: [ [object Object] ],
// name: 'Aaron',
// age: 100,
// _id: 4e56698f15dff83410000001
// }
// person.stories = {
// fans: [ ],
// _id: 4e56698f15dff83410000002,
// _creator: 4e56698f15dff83410000001,
// title: 'A man who cooked Nintendo'
// }
// story = {
// fans: [ ],
// _id: 4e56698f15dff83410000002,
// _creator:
// { stories: [ 4e56698f15dff83410000002 ],
// name: 'Aaron',
// age: 100,
// _id: 4e56698f15dff83410000001 },
// title: 'A man who cooked Nintendo'
// }
// populates a single object
User.findById(id, function (err, user) {
var opts = [
{ path: 'company', match: { x: 1 }, select: 'name' }
, { path: 'notes', options: { limit: 10 }, model: 'override' }
]
User.populate(user, opts, function (err, user) {
console.log(user);
})
})
// populates an array of objects
User.find(match, function (err, users) {
var opts = [{ path: 'company', match: { x: 1 }, select: 'name' }]
var promise = User.populate(users, opts);
promise.then(console.log).end();
})
// imagine a Weapon model exists with two saved documents:
// { _id: 389, name: 'whip' }
// { _id: 8921, name: 'boomerang' }
var user = { name: 'Indiana Jones', weapon: 389 }
Weapon.populate(user, { path: 'weapon', model: 'Weapon' }, function (err, user) {
console.log(user.weapon.name) // whip
})
// populate many plain objects
var users = [{ name: 'Indiana Jones', weapon: 389 }]
users.push({ name: 'Batman', weapon: 8921 })
Weapon.populate(users, { path: 'weapon' }, function (err, users) {
users.forEach(function (user) {
console.log('%s uses a %s', users.name, user.weapon.name)
// Indiana Jones uses a whip
// Batman uses a boomerang
})
})
// Note that we didn't need to specify the Weapon model because
// we were already using it's populate() method.
// query
db.users.find({age: {$gte: 18, $lte: 30}}) // lt,gt
db.users.find({age: {$in: [12, 'ud']]}}) // $nin
db.food.insert({'fruit': ['apple', 'banana', 'peach']})
db.food.find({'fruit': 'apple'}) // match docs that have apple in it
db.food.find({'fruit': {$all: ['apple', 'banana']}) // match docs that have both of them
// Model.update
var conditions = { name: 'borne' }
var update = { $inc: { visits: 1 }}
var options = { multi: true }
Model.update(conditions, update, options, callback);
function callback (err, numAffected) {
// numAffected is the number of updated documents
})
var query = { name: 'borne' }
Model.update(query, { name: 'jason borne' }, options, callback)
// Equals to
Model.update(query, { $set: { name: 'jason borne' }}, options, callback)
Model.findOne({ name: 'borne' }, function (err, doc){
doc.name = 'jason borne';
doc.visits.$inc();
doc.save();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment