簡體   English   中英

將數據從一個模型分為兩個模型后,如何重寫貓鼬查詢?

[英]how can I rewrite my mongoose query after splitting data from one model into two?

在我的應用程序中,我存儲評論。 以前,我的模型如下所示:

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}
}

每個評論(除了存儲其詳細信息外)還具有有關作者的詳細信息,例如, usernamefacebook_username ,添加評論的device_iddisplay_name 我還基於一個bool標志friends_only來決定是否只對用戶的Facebook朋友或所有人可見該評論。

用於獲取所有注釋的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)

使用上面的代碼,用戶可以獲取他的朋友發布的內容(設置為公開或私有)以及所有其他公開內容(其他所有人)。

我決定更改所有這些內容並將數據分為兩個模型。 更改后,我有:

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部分:

...
var finalQuery = Comment.find(query)

finalQuery.populate({path: 'user_id', 
    select: 'facebook_username display_name username',
    match: {

}});

我不知道該怎么做。 你能幫我嗎?

首先,我建議您使用填充查詢,如果您認為填充不會提供所需的數據,則可以運行兩個查詢並合並這些結果。

對於填充,我從貓鼬的官方文檔中找到了解決方案。 你可以這樣

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"
});

這是doc鏈接: http : //mongoosejs.com/docs/populate.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM