【发布时间】:2016-06-18 03:42:36
【问题描述】:
我想在 nodejs 应用程序中获取猫鼬的查询,如下所述。
user.js、comment.js 和 post.js 是我使用的模型文件。
user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var userSchema = new Schema({
nick_name:{type:String},
email: {
type: String,
trim: true,
required: '{PATH} is required!',
index: true,
},
},{ collection: 'user'});
var User = mongoose.model('User', userSchema);
module.exports = User;
comment.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var commentSchema = new Schema({
comment: type:String,
user_id:{
type:Schema.Types.ObjectId, ref:'User'
},
is_active :1
},{ collection: 'comment'});
post.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var postSchema = new Schema({
post: type:String,
user_id:{
type:Schema.Types.ObjectId, ref:'User'
},
is_active :1
},{ collection: 'post'});
想出来如下:
{
"nick_name":"prakash",
"email":"prakash@mailinator.com",
"comments":[
{
"comment":"this is a comment text1",
"is_active":1,
},
{
"comment":"this is a comment text2",
"is_active":1,
}
],
"posts":[
{
"post":"this is a post text1",
"is_active":1,
},
{
"post":"this is a post text2",
"is_active":1,
},
{
"post":"this is a post text3",
"is_active":1,
},
]
}
依赖项
"express" => "version": "4.7.4",
"mongoose" => "version": "4.4.5",
"mongodb" => "version": "2.4.9",
"OS" => "ubuntu 14.04 lts 32bit",
如果无法查询,请建议我一个合适的猫鼬插件。 但我不想对 user.js 文件及其 userSchema 对象进行任何更改。
【问题讨论】:
-
@BlakesSeven - 哈哈 Simplez...
-
我现在也在苦苦挣扎,我有评论对象,Like 对象,还有其他对象。我想向用户个人资料页面显示他所做的所有活动。但关键是它们是不同的集合。如果我单独做,那么它们将不会按时间排序
-
我确实在“猫鼬的查询”中看到了幽默,但对@laxman 的支持,我只精通一种语言。用英语表达这个你可能会说,'我想在 nodejs 应用程序中使用 Mongoose 查询 Mongo 数据库......'。