Luego de tanto batallar hemos dado con la respuesta de esta impedimento que agunos usuarios de nuestro sitio web han presentado. Si tienes algún detalle que aportar puedes aportar tu comentario.
Ejemplo 1: poblado de mangostas
Story.findOne( title:/casino royale/i).populate('author','name').// only return the Persons nameexec(function(err, story)if(err)returnhandleError(err);console.log('The author is %s', story.author.name);// prints "The author is Ian Fleming"console.log('The authors age is %s', story.author.age);// prints "The authors age is null');
Ejemplo 2: poblado de mangostas
//EXAMPLE ONEconst mongoose =require('mongoose')const shortid =require('shortid')/**
* @description setup database connection
*/
mongoose
.connect('mongodb://localhost:27017/mongoose',
useFindAndModify:false,
useUnifiedTopology:true,
useNewUrlParser:true).then(()=>console.log('Connected')).catch(()=>console.log('Not Connected'))/**
* @description user mongoose schema
*/constUserSchema=newmongoose.Schema(
fullName:
type:String,
trim:true,
required:true,
courseId:
type:String,
ref:'Course',default: shortid.generate, timestamps:true)constUser= mongoose.model('User',UserSchema,'User')/**
* @description course mongoose schema
*/constCourseSchema=newmongoose.Schema(
userId: type: mongoose.Types.ObjectId,
course:[], timestamps:true)constCourse= mongoose.model('Course',CourseSchema)/**
* @description register user account
*/asyncfunctionregisterAccount()const user =awaitUser.create( fullName:'john doe')awaitCourse.create( userId: user._id)console.log('register successfuly')
process.exit(0)//registerAccount/**
* @description buy video course
*/asyncfunctionbuyCourse()const user =awaitUser.findOne( fullName:'john doe')const course =awaitCourse.updateOne( userId: user._id,
$push:
course:
$each:[ name:'react for beginner', price:95000, name:'vue for beginner', price:85000, name:'angular for beginner', price:75000])console.log('sucessfuly to buy course')
process.exit(0)// buyCourse()/**
* @description results all course after buy course
*/asyncfunctionresultsCourse()const user =awaitUser.findOne( fullName:'john doe')const courseResult =awaitCourse.findOne( userId: user._id).populate(
path:'userId',
select:'course firstName lastName').lean()console.log(
fullname: user.fullName,
courses: courseResult.course)
process.exit(0)resultsCourse()
Reseñas y puntuaciones
Recuerda que te brindamos la opción de aclarar tu experiencia si encontraste tu duda .
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)