/*
db.order.aggregate([
{
$lookup:
{
from: "order_item",
localField: "order_id",
foreignField: "order_id",
as: "items"
}
},
{
$match:{"all_price":{$gte:90}}
}
])
*/
var ArticleModel=require('./model/article.js');
//查询文章信息
/*
ArticleModel.find({},function(err,docs){
console.log(docs);
})
*/
//查询文章信息 并显示文章的分类 以及文章的作者信息
//两个表关联查询
/*
ArticleModel.aggregate([
{
$lookup: {
from: "articlecate",
localField: "cid",
foreignField: "_id",
as: "cate"
}
}
],function(err,docs){
console.log(docs[2].cate)
})
*/
//三个表关联查询
ArticleModel.aggregate([
{
$lookup: {
from: "articlecate",
localField: "cid",
foreignField: "_id",
as: "cate"
}
},
{
$lookup: {
from: "user",
localField: "author_id",
foreignField: "_id",
as: "user"
}
}
],function(err,docs){
console.log(JSON.stringify(docs));
})
var ArticleCateModel=require('./model/articlecate.js');
var UserModel=require('./model/user.js');
var ArticleModel=require('./model/article.js');
//分类的增加
// var cate=new ArticleCateModel({
// title:'国内新闻',
// description:'国内新闻'
// })
// cate.save();
//增加用户
// var user= new UserModel({
// username :'lisi',
// password:'13214lkisisgfdsgsdsg',
// name:'李四',
// age:20,
// sex:'男',
// tel:124212142151
// })
// user.save();
var article=new ArticleModel();
article.title="这是一个新闻11111111"
article.cid='5b7900bbf3965813d41216c1'; /*国内新闻*/
article.author_id='5b7901332d552617b09af422';
article.author_name='张三';
article.descripton='这是一个新闻11111111 此处省略300字';
article.content='jhhhhhhhhh 这是一个新闻11111111'
article.save();
var mongoose=require('./db.js');
var UserSchema = new mongoose.Schema({
username : { type: String, unique: true },
password:String,
name:String,
age:Number,
sex:String,
tel:Number,
status:{
type:Number,
default:1
}
});
module.exports=mongoose.model('User',UserSchema,'user');
var mongoose=require('./db.js');
var ArticleCateSchema = new mongoose.Schema({
title : { type: String, unique: true },
descripton:String,
addtime:{
type:Date
}
});
module.exports=mongoose.model('ArticleCate',ArticleCateSchema,'articlecate');
var mongoose=require('./db.js');
var Schema=mongoose.Schema;
var ArticleSchema = new Schema({
title:{
type: String, unique: true
},
cid : {
type: Schema.Types.ObjectId
}, /*分类 id*/
author_id:{
type: Schema.Types.ObjectId
}, /*用户的id*/
author_name:{
type:String
},
descripton:String,
content : String
});
module.exports=mongoose.model('Article',ArticleSchema,'article');