[英]how can I rewrite my mongoose query after splitting data from one model into two?
In my application I store comments. 在我的应用程序中,我存储评论。 Previously my model for that looked like this:
以前,我的模型如下所示:
var CommentsSchema = new Schema({
username: {type: String},
display_name: {type: String},
facebook_username: {type: String},
text_content: {type: String},
photo_content_url: {type: String},
hashtags: {type: [String]},
device_id: {type: String},
comment_date: {type: Date, default: Date.now},
friends_only: {type: Boolean, default: false}
}
Each comment - besides storing its details - had also details about the author, eg username
, facebook_username
, device_id
from which the comment was added and display_name
. 每个评论(除了存储其详细信息外)还具有有关作者的详细信息,例如,
username
, facebook_username
,添加评论的device_id
和display_name
。 There was also a bool flag friends_only
based on which I was deciding whether that comment should be visible only to user's facebook friends or to everyone. 我还基于一个bool标志
friends_only
来决定是否只对用户的Facebook朋友或所有人可见该评论。
Construction of the node.js
/ mongoose
query for fetching all comments looked like this: 用于获取所有注释的
node.js
/ mongoose
查询的构造如下所示:
commentsRoutes.post('/friends', function (req, res) {
var friends = req.body.friends;
var publicComments = req.body.publicComments;
var hashtagsInput = req.body.hashtags;
var startDate = req.body.startDate;
var endDate = req.body.endDate;
var query= {};
query.$and = [];
// and condition on start date
if(startDate != undefined) {
var startDate = new Date(req.param('startDate'));
var endDate = new Date(req.param('endDate'));
query.$and.push({"comment_date":{$gte: startDate}});
query.$and.push({"comment_date":{$lte: endDate}});
}
// and condition on hastags
if (hashtagsInput != undefined) {
var hashtags = hashtagsInput.split(",");
query.$and.push({"hashtags":{$in: hashtags}});
}
// creating a OR condition for facebook friends and public flag
var friend_query = {};
friend_query.$or = [];
if (friends != undefined) {
var friendsSplit = friends.split(",");
friend_query.$or.push({"facebook_username":{$in: friendsSplit}});
}
if (publicComments != undefined && publicComments === "true") {
friend_query.$or.push({friends_only: false});
}
//Merging facebook friend condition with other condition with AND operator.
query.$and.push(friend_query);
var finalQuery = Comment.find(query)
With the code above user could fetch content posted by his friends (that was set either to public or private) and all other public content (from everyone else). 使用上面的代码,用户可以获取他的朋友发布的内容(设置为公开或私有)以及所有其他公开内容(其他所有人)。
I've decided to change all of that and split the data into two models. 我决定更改所有这些内容并将数据分为两个模型。 After changing it I have:
更改后,我有:
var CommentsSchema = new Schema({
user_id: {type: String, required: true, ref: 'users' },
text_content: {type: String},
photo_content_url: {type: String},
hashtags: {type: [String]},
comment_date: {type: Date, default: Date.now},
friends_only: {type: Boolean, default: false},
device_id: {type: String}
}
and 和
var UsersSchema = new Schema({
username: {type: String},
facebook_username: {type: String},
display_name: {type: String}
}
Now, when I want to keep the old functionality, I need to modify the code responsible for creating the query. 现在,当我想保留旧功能时,需要修改负责创建查询的代码。 I could merge two queries with
async
, or the other way is to use mongoose
.populate
option. 我可以合并两个查询与
async
,或另一种方式是用mongoose
.populate
选项。 I decided to go with the second choice, so now I need to move the code responsible for creating or
query to the match
part of populate
function: 我决定采用第二种选择,因此现在我需要将负责创建
or
查询的代码移到populate
函数的match
部分:
...
var finalQuery = Comment.find(query)
finalQuery.populate({path: 'user_id',
select: 'facebook_username display_name username',
match: {
}});
I don't know how to do it. 我不知道该怎么做。 Can you help me with that?
你能帮我吗?
First, i suggest you that go with a populate query, if you feel that populate won't give you a data that you need that you can run two queries and merge those results. 首先,我建议您使用填充查询,如果您认为填充不会提供所需的数据,则可以运行两个查询并合并这些结果。
for populate, i found the solution from the official doc of mongoose. 对于填充,我从猫鼬的官方文档中找到了解决方案。 you can do like this.
你可以这样
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
Story
.findOne({ title: 'Once upon a timex.' })
.populate('_creator')
.exec(function (err, story) {
if (err) return handleError(err);
console.log('The creator is %s', story._creator.name);
// prints "The creator is Aaron"
});
here is doc link: http://mongoosejs.com/docs/populate.html 这是doc链接: http : //mongoosejs.com/docs/populate.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.