Last active
January 21, 2020 10:12
-
-
Save aheckmann/4947012 to your computer and use it in GitHub Desktop.
Mongoose 3.6 population example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=========== | |
mongoose version: 3.6.0rc0 | |
======== | |
dbname: testing_populateAdInfinitum | |
[ { title: 'blog 1', | |
author: | |
{ _id: 511bde3e3985283f25000004, | |
name: 'sally', | |
__v: 0, | |
friends: | |
[ { _id: 511bde3e3985283f25000001, name: 'mary' }, | |
{ _id: 511bde3e3985283f25000002, name: 'bob' } ] }, | |
_id: 511bde3e3985283f25000005, | |
tags: [ 'fun', 'cool' ], | |
__v: 0 }, | |
{ title: 'blog 3', | |
author: | |
{ _id: 511bde3e3985283f25000003, | |
name: 'joe', | |
__v: 0, | |
friends: | |
[ { _id: 511bde3e3985283f25000001, name: 'mary' }, | |
{ _id: 511bde3e3985283f25000002, name: 'bob' } ] }, | |
_id: 511bde3e3985283f25000007, | |
tags: [ 'fun', 'odd' ], | |
__v: 0 } ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var ObjectId = mongoose.Types.ObjectId; | |
console.log('\n==========='); | |
console.log(' mongoose version: %s', mongoose.version); | |
console.log('========\n\n'); | |
var dbname = 'testing_populateAdInfinitum'; | |
console.log('dbname: %s', dbname); | |
mongoose.connect('localhost', dbname); | |
mongoose.connection.on('error', function () { | |
console.error('connection error', arguments); | |
}); | |
var user = new Schema({ | |
name: String | |
, friends: [{ type: Schema.ObjectId, ref: 'User' }] | |
}); | |
var User = mongoose.model('User', user); | |
var blogpost = Schema({ | |
title: String | |
, tags: [String] | |
, author: { type: Schema.ObjectId, ref: 'User' } | |
}) | |
var BlogPost = mongoose.model('BlogPost', blogpost); | |
mongoose.connection.on('open', function () { | |
var userIds = [new ObjectId, new ObjectId, new ObjectId, new ObjectId]; | |
var users = []; | |
users.push({ | |
_id: userIds[0] | |
, name: 'mary' | |
, friends: [userIds[1], userIds[2], userIds[3]] | |
}); | |
users.push({ | |
_id: userIds[1] | |
, name: 'bob' | |
, friends: [userIds[0], userIds[2], userIds[3]] | |
}); | |
users.push({ | |
_id: userIds[2] | |
, name: 'joe' | |
, friends: [userIds[0], userIds[1], userIds[3]] | |
}); | |
users.push({ | |
_id: userIds[3] | |
, name: 'sally' | |
, friends: [userIds[0], userIds[1], userIds[2]] | |
}); | |
User.create(users, function (err, docs) { | |
assert.ifError(err); | |
var blogposts = []; | |
blogposts.push({ title: 'blog 1', tags: ['fun','cool'], author: userIds[3] }) | |
blogposts.push({ title: 'blog 2', tags: ['cool'], author: userIds[1] }) | |
blogposts.push({ title: 'blog 3', tags: ['fun','odd'], author: userIds[2] }) | |
BlogPost.create(blogposts, function (err, docs) { | |
assert.ifError(err); | |
BlogPost.find({ tags: 'fun' }).lean().populate('author').exec(function (err, docs) { | |
assert.ifError(err); | |
var opts = { | |
path: 'author.friends' | |
, select: 'name' | |
, options: { limit: 2 } | |
} | |
BlogPost.populate(docs, opts, function (err, docs) { | |
assert.ifError(err); | |
console.log(); | |
console.log(docs); | |
done(); | |
}) | |
}) | |
}) | |
}) | |
}); | |
function done (err) { | |
if (err) console.error(err.stack); | |
mongoose.connection.db.dropDatabase(function () { | |
mongoose.connection.close(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var addContent= mongoose.Schema({
userId:{type:Number},
contentId:{type:Number,index:{unique:true}},
contentType:{type:String},
contentTitle:{type:String},
description:{type:String},
syncTime:{type:Date ,default:Date.now,required:true},
content:{type:String,contentType:String},
lastViewed:{type:Date,default:Date.now,required:true},
action:{type:String},
var addContent=mongoose.model('addContent',addContent,'addContent');
exports.addContent=addContent;
var user= mongoose.Schema({
userId:{type:Number,index:{unique:true}},
username:{type:String,required:true},
mobile:{type:String ,required:true},
active:{type:Boolean,default:false},
otp:{type:Number},
syncTime:{type:Date ,default:Date.now,required:true},
image:{data:Buffer,contentType:String}
var User=mongoose.model('User',user,'users');
exports.User=User;
when add content by User it automatically update in User collection ....
how can do this???????