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?
我有下面的代码在node js使用猫鼬包。
1)如果我使用填充方法,我如何在响应的基础数组中删除字段总数?因为该数组是由对另一个集合的引用组成的。
2)我如何使用"活动"。字段来过滤只有"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);
在campaign模型中,我有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"
]
}
]
期望的响应(只是想在活动对象中删除基础数组字段):
[
{
"_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
}
]
任何想法?。非常感谢!!
EDIT:为了将来的参考,使用填充来过滤"campaign"JSONArray上具有state="Active"的对象,我实现了使用:var =结果Foundation.find ({}, {twitter_account: 1、名称:1、活动:1})
.populate({path : 'campaigns' ,select :'-foundations', match:{state:'ACTIVE'}}).exec(function (err, foundation) {
if (err) return handleError(err);
console.log(foundation);
res.send(foundation);
});
您可以使用populate with 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
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- JavaScript数组包含一个值
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- jQuery$.inArray()总是返回-1和一个对象数组
- 在数组中的一个元素上设置多个值
- javascript处理一个对象数组以获得一个新的对象数组
- 作为一个二维数组,从ajax接收
- 你能用来自数组的属性名称生成一个对象吗
- 多维关联数组的最后一个索引
- 如何创建一个方法来验证数组的范围
- 循环以检查数组中的最后一个图像
- 在Javascript中将一个值和字符串数组转换为if语句
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- 如何将一个对象添加到每个对象数组中
- 如何创建一个谷歌地图地理坐标数组
- 如何从另一个带下划线的数组中筛选带元素的数组
- 使用window.location.htm和匹配的URL数组(一个用于桌面,一个用于移动)将桌面网站重定向到移动
- Javascript排序多维数组-一个完整的例子
- 刽子手的游戏.2数组.一个需要相应地更新另一个