我有两个集合,一个用于用户,另一个用于帖子,我正在尝试获取用户名,例如谁在页面上发布了Feed。我现在得到的东西是 IMAGE
并希望实现像twitter这样的东西。但由于用户数据和帖子在两个不同的集合中。我只能在帖子上获得“_id”。以下是文件
模型/ story.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var StorySchema = new Schema({
creator: {type:Schema.Types.ObjectId, ref: 'User'},
content: String,
create: { type:Date, default: Date.now }
});
module.exports = mongoose.model('Story', StorySchema);
模型/ user.js的
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name:String,
username: { type:String, required: true, index: { unique: true}},
password: { type: String, reuired: true, select: false }
});
UserSchema.pre('save', function(next){
var user = this;
if(!user.isModified('password')) return next();
bcrypt.hash(user.password, null, null, function(err, hash){
if(err) return next(err);
user.password = hash;
next();
});
});
UserSchema.methods.comparePassword = function(password){
var user = this;
return bcrypt.compareSync(password, user.password);
}
module.exports = mongoose.model('User', UserSchema);
controllers / Storyctrl.js
angular.module('storyCtrl', ['storyService'])
.controller('StoryController', function(Story, socketio){
vm = this;
vm.stories=[];
vm.getStories = function(){
Story.all()
.success(function(data){
vm.stories = data;
});
};
vm.createStory = function(){
vm.message = '';
Story.create(vm.storyData)
.success(function(data){
vm.storyData.content = '';
vm.message = data.message;
// vm.getStories(); // Refresh
// vm.stories.push(vm.storyData);
});
};
vm.getStories();
socketio.on('story', function(data){
vm.stories.push(data);
});
})
.controller('allStoriesController', function(stories, socketio){
var vm = this;
vm.stories = stories.data;
socketio.on('story', function(data){
vm.stories.push(data);
});
})
Html文件
<div>
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Stories</h4>
</div>
<div class="panel-body">
<ul class="list-group" >
<li class="list-group-item" ng-repeat="s in story.stories | reverse">
{{ s.content | date:"h:mma 'on' MMM d, y" }}
<small>Posted by @ {{s.creator}}</small>
<small class="pull-right">{{s.create | date:"h:mma 'on' MMM d, y"}}</small>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
我是nodejs和mongodb的新手,如果有一个小错误或我尚未涉及的内容,请在评论中饶恕我。
答案 0 :(得分:3)
在nodejs中,您必须使用带参考的对象的填充。 它是一个ORM功能,而不是mongodb原生功能,因此在使用 mongoose ORM 时,您必须使用引用ID填充对象,并且填充对象在查询完成后将json变量替换为对象。 有关详细信息,请访问此链接:
http://mongoosejs.com/docs/populate.html
这是您必须在nodejs路由文件
中编写的代码<强> TestPopulation.js 强>
var mongoose = require('mongoose');
require('./connection');
// require mongoose models
var Story = mongoose.model('Story');
var Person = mongoose.model('Person');
Person.find().populate({
path: 'stories',
// Get friends of friends - populate the 'friends' array for every friend
populate: { path: '_creator' }
}).exec(function(err,data){
console.log(data);
});
<强> connection.js 强>
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var personSchema = new Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = new Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
mongoose.model('Story', storySchema);
mongoose.model('Person', personSchema);
//mongoose runs only on 27017 port
mongoose.connect('mongodb://127.0.0.1:27017/spicex');
<强>输出强>
[{
"_id": 0,
"name": "Aaron",
"age": 100,
"__v": 0,
"stories": [{
"_id": "584d083038c162c81a69a013",
"title": "Once upon a timex.",
"_creator": {
"_id": 0,
"name": "Aaron",
"age": 100,
"__v": 0,
"stories": ["584d083038c162c81a69a013"]
},
"__v": 0,
"fans": []
}]
}, {
"_id": 1,
"name": "sanjeev",
"age": 100,
"__v": 0,
"stories": []
}]