【问题标题】: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?mongoose.js:如何在使用填充和使用子数组字段之一作为过滤器时从引用子数组中删除属性?
【发布时间】:2023-03-18 10:40:02
【问题描述】:

我在使用 mongoose 包的节点 js 中有以下代码。

1)如果我使用填充方法,如何删除响应中基础数组中的字段总计?因为该数组是由对另一个集合的引用形成的。

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 字段,它是对基金会的引用数组,我想在响应中将其删除以避免冗余

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

期望的响应(只想在活动对象中删除基础数组字段):

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

【问题讨论】:

    标签: javascript mongoose nosql


    【解决方案1】:

    您可以通过 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);
            });
    

    只需浏览 mongoose 文档 http://mongoosejs.com/docs/populate.html

    【讨论】:

    • 很好地回答了我的朋友....我阅读了有关填充的文档并完成了工作...我编辑了原始帖子以留下有关如何过滤 JSON 数组属性时的答案您使用填充。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2016-03-03
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多