MongoDB:投影到数组中所有对象的特定字段

时间:2017-01-31 12:34:25

标签: mongodb mongodb-query

我有一个MongoDB数据库,其中包含一个Part的以下数据结构: Part Data structure

现在我想对数组Description中每个对象的元素SensorData进行投影。使用带有C#的.NET中的MonogDB API,我的代码将如下所示:

var projection = Builders<Part>.Projection
                .Include(part => part.SensorData[0].Description)
                .Include(part => part.SensorData[1].Description)
                .Include(part => part.SensorData[2].Description)
                //...   
                ;

问题是SensorData中的对象数是动态的,可能介于0到大约20之间,因此不可能包括每个Description字段。投影必须在服务器端完成,因为SensorData -> Values可能很大。

是否有任何语法或方法可以进行此类投影?

1 个答案:

答案 0 :(得分:0)

刚问了一位同事之后找到了一个解决方案:使用[self beginShow:5 myIncrease:0.1]; - (void)beginShow:(CGFloat)duration myIncrease:(CGFloat)myIncrease{ CGFloat currentTime = 0.0; while(duration > currentTime){ [self performSelector:@selector(setRed) withObject:nil afterDelay:currentTime]; [self performSelector:@selector(setTorchToLevelOn) withObject:nil afterDelay:currentTime]; currentTime = currentTime+myIncrease; [self performSelector:@selector(setBlack) withObject:nil afterDelay:currentTime]; [self performSelector:@selector(setTorchToLevelOff) withObject:nil afterDelay:currentTime]; currentTime = currentTime+myIncrease; [self performSelector:@selector(setPurple) withObject:nil afterDelay:currentTime]; [self performSelector:@selector(setTorchToLevelOn) withObject:nil afterDelay:currentTime]; currentTime = currentTime+myIncrease; [self performSelector:@selector(setDefault) withObject:nil afterDelay:currentTime]; [self performSelector:@selector(setTorchToLevelOff) withObject:nil afterDelay:currentTime]; currentTime = currentTime+myIncrease; } } 进行此操作是不可能的,但它与Projection Builder一起使用:

aggregation framework