{
"_id": newDate("2/5/2015 15:00:18"),
"bidPrices": [
13.78,
13.77,
13.76,
13.75,
13.74,
13.73,
13.72,
13.71,
13.7,
13.69
],
"askPrices": [
13.79,
13.8,
13.81,
13.82,
13.83,
13.84,
13.85,
13.86,
13.87,
13.88
]
}
我想从bidPrices获得哪些索引为“1,3,5,6”的价格。 我所知道的是使用$ slice,mongo将返回bidPrices的子数组。 有没有办法让mongo返回这样的数组:
[0, 13.77, 0, 13.75, 0, 13.73, 13.72, 0, 0, 0]
谢谢!
答案 0 :(得分:0)
使用Map Reduce
var mapFunction = function(){
for (var i in this.bidPrices) {
if(i == 1 || i == 3 || i == 5 || i == 6){
emit(this._id, this.bidPrices[i]);
}
};
};
var reduceFunction = function(key, values) {
var reduced = {};
for (var i in values) {
reduced[i] = values[i];
};
return reduced;
};
db.test2.mapReduce(
mapFunction,
reduceFunction,
{ out: "test2_res"
}
)