Usually there is a relationship between different sets of data, for example, in a different set, but when a user article published articles and user information storage, to query all of the information published in the article include users, you need to use a set of associations.
Articles collection | User Collections |
_id | _id |
title | name |
author | age |
content |
hobbies |
Example:
the require Mongoose = const ( 'Mongoose') mongoose.connect ( 'MongoDB: // localhost / test1', {useNewUrlParser: to true}) .then (() => the console.log ( 'database connection success')) .catch ( err => console.log ( 'data connection' + ERR)) // set of rules article const = PostSchema new new mongoose.Schema ({ title: String, Content: String, author: { type: mongoose.Schema.Types.ObjectId , REF: 'the user' } }) // user set rules const = UserSchema new new mongoose.Schema ({ name: String, Age: Number The, hobbies: String }) // collection of articles const post = mongoose.model ( 'Post' , PostSchema) // user collections User mongoose.model = const ( 'the User', UserSchema) // Create User user.create (name {: 'zhangsan', Age: 20 is, hobbies: '11111'}) .then (RES => the console.log (RES )) .catch (ERR => the console.log (ERR)) // // Create Base post.create ({title: 'test test', content: 'contents contents', author: '5d34f0542fdc3f7924249a9c'}) . the then (RES => the console.log (RES)) .catch (ERR => the console.log (ERR)) // query post.find (). populate ( 'author '). then (res => console.log ( res))