簡體   English   中英

mongoose.js:如何在使用填充時從引用的子數組中刪除屬性,並將子數組字段之一用作過濾器?

[英]mongoose.js: How do I remove a property from a subarray of references while using populate and use one of the subarray field as a filter as well?

我在使用貓鼬包的節點js中有以下代碼。

1)如果我使用populate方法,如何在響應的Foundations數組中刪除字段總數? 導致該數組由對另一個集合的引用形成。

2)如何使用“ campaigns.state”字段僅過濾以“ ACTIVE”為狀態的廣告系列?

定義:

基金會模型

var mongoose=require('mongoose');
var Schema = mongoose.Schema;

var foundation = new Schema({

     twitter_account:{type: String, required: true, unique: true},

     name:{type: String, required: true, unique: true},

     pic_path: {type: String, required: true},

     doc_path: {type: String, required: true},

     campaigns: [{type: Schema.ObjectId, ref: 'Campaign'}]
},{
    timestamps: true
});

module.exports = mongoose.model('Foundation', foundation);

在Campaigns模型上,我有Foundations字段,該字段是對Foundations的引用數組,我想在響應中將其切出以避免冗余

var campaign = new Schema({

.............

     foundations: [{type: Schema.ObjectId, ref: 'Foundation'}],

......................
}

在這里檢索數據的API路線

var express = require('express');
var router = express.Router();
var Foundation=require('../models/Foundation');
var mongoose= require('mongoose');
var Promise = require("bluebird");
var _ = require('lodash');


//get all Foundations
router.get('/get/all', function(req, res, next) {

  var results=Foundation.find({},{twitter_account:1,name:1,campaigns:1})

  .populate('campaigns').exec(function (err, foundation) {
        if (err) return handleError(err);
        console.log(foundation);
        res.send(foundation);
});

});

module.exports = router;

這是響應:

[
  {
    "_id": "58229b253e1ad628e1ed975e",
    "twitter_account": "@YYYYYYYYYYYY",
    "name": "YYYYYYYYYYYY",
    "campaigns": [
      {
        "_id": "5822a9444aea133044b6eb84",
        "updatedAt": "2016-11-09T04:42:44.922Z",
        "createdAt": "2016-11-09T04:42:44.922Z",
         "total": 0,
        "state": "ACTIVE",
        "__v": 0,
        "end_date": null,
        "foundations": [
          "58229b253e1ad628e1ed975e",
          "58229b253e1ad628e1ed975f"
        ]
      },
      {
        "_id": "5822a9444aea133044b6eb85",
        "updatedAt": "2016-11-09T04:42:44.925Z",
        "createdAt": "2016-11-09T04:42:44.925Z",
        "remaining_ammount_to_goal": 500000,
        "total": 0,
        "state": "ACTIVE",
        "__v": 0,
        "end_date": null,
        "foundations": [
          "58229b253e1ad628e1ed975e"
        ]
      }
    ]
  },
  {
    "_id": "58229b253e1ad628e1ed975f",
    "twitter_account": "@XXXXXXXXXXX",
    "name": "XXXXXXXXX",
    "campaigns": [
      {
        "_id": "5822a9444aea133044b6eb85",
        "updatedAt": "2016-11-09T04:42:44.925Z",
        "createdAt": "2016-11-09T04:42:44.925Z",
        "total": 0,
        "state": "ACTIVE",
        "__v": 0,
        "end_date": null,
        "foundations": [
          "58229b253e1ad628e1ed975e"
        ]
      }
    ]

所需的響應(只想在Campaigns對象中放置基礎數組字段):

[
  {
    "_id": "58229b253e1ad628e1ed975e",
    "twitter_account": "@YYYYYYYYYYYY",
    "name": "YYYYYYYYYYYY",
    "campaigns": [
      {
        "_id": "5822a9444aea133044b6eb84",
        "updatedAt": "2016-11-09T04:42:44.922Z",
        "createdAt": "2016-11-09T04:42:44.922Z",
         "total": 0,
        "state": "ACTIVE",
        "__v": 0,
        "end_date": null
      },
      {
        "_id": "5822a9444aea133044b6eb85",
        "updatedAt": "2016-11-09T04:42:44.925Z",
        "createdAt": "2016-11-09T04:42:44.925Z",
        "remaining_ammount_to_goal": 500000,
        "total": 0,
        "state": "ACTIVE",
        "__v": 0,
        "end_date": null
      }
    ]
  },
  {
    "_id": "58229b253e1ad628e1ed975f",
    "twitter_account": "@XXXXXXXXXXX",
    "name": "XXXXXXXXX",
    "campaigns": [
      {
        "_id": "5822a9444aea133044b6eb85",
        "updatedAt": "2016-11-09T04:42:44.925Z",
        "createdAt": "2016-11-09T04:42:44.925Z",
        "total": 0,
        "state": "ACTIVE",
        "__v": 0,
        "end_date": null
      }
    ]

有任何想法嗎?。 非常感謝你!!!

編輯 :供以后參考,使用填充過濾器在“ Campaigns” JSONArray上具有state =“ Active”的對象,我使用以下方法實現了此目的:var results = Foundation.find({},{twitter_account:1,name:1,campaigns :1})

  .populate({path : 'campaigns' ,select :'-foundations', match:{state:'ACTIVE'}}).exec(function (err, foundation) {
        if (err) return handleError(err);
        console.log(foundation);
        res.send(foundation);
});

您可以使用select填充

       var results=Foundation.find({},{twitter_account:1,name:1,campaigns:1})
          .populate({path : 'campaigns' ,select :'-foundations'})
          .exec(function (err, foundation) {
                if (err) return handleError(err);
                console.log(foundation);
                res.send(foundation);
        });

只需瀏覽貓鼬文檔http://mongoosejs.com/docs/populate.html

暫無
暫無

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

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