将数据从一个模型分为两个模型后,如何重写猫鼬查询? [英] 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}
}
每个评论-除了存储其详细信息之外-还包含有关作者的详细信息,例如从中添加注释的username
,facebook_username
,device_id
和display_name
.还有一个bool标志friends_only
,根据该标志,我决定是只对用户的Facebook朋友还是所有人都可见该评论.
Each comment - besides storing its details - had also details about the author, e.g. username
, facebook_username
, device_id
from which the comment was added and 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.
用于提取所有注释的node.js
/mongoose
查询的构造如下:
Construction of the node.js
/mongoose
query for fetching all comments looked like this:
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}
}
和
var UsersSchema = new Schema({
username: {type: String},
facebook_username: {type: String},
display_name: {type: String}
}
现在,当我想保留旧功能时,需要修改负责创建查询的代码.
我可以将两个查询与async
合并,或者另一种方法是使用mongoose
.populate
选项.我决定采用第二种选择,因此现在我需要将负责创建or
查询的代码移到populate
函数的match
部分:
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. 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:
...
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"
});
这是文档链接: http://mongoosejs.com/docs/populate.html
这篇关于将数据从一个模型分为两个模型后,如何重写猫鼬查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!