假设我们有包含以下形状文档的mongo集合:
-(void)createSomeLabel {
// Create and position my label
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
0,
self.view.frame.size.width - 40.0,
self.view.frame.size.height - 300.0)];
someLabel.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
someLabel.textAlignment = NSTextAlignmentCenter;
someLabel.textColor = [UIColor whiteColor];
someLabel.lineBreakMode = NSLineBreakByWordWrapping;
someLabel.numberOfLines = 0;
[self.view addSubview:someLabel];
// This string will be different lengths all the time
NSString *someLongString = @"Here is a really long amount of text that is going to wordwrap/line break and I don't want to highlight the spacing. I want to just highlight the words and a single space before/after the word";
// Create attributed string
NSMutableAttributedString *someLongStringAttr = [[NSMutableAttributedString alloc] initWithString:someLongString attributes:nil];
// Apply background color
[someLongStringAttr addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithWhite:0 alpha:0.25]
range:NSMakeRange(0, someLongStringAttr.length)];
// Set text of label
someLabel.attributedText = someLongStringAttr;
}
但有时 {
_id: '1234',
letters: ['a', 'b', 'c', 'd'],
}
字段可能是letters
。
我想知道是否可以在单个查询中实现以下功能
null
数组非空,请将第一项设置为“x”。letters
字段设置为letters
。当然,最简单的想法不起作用
['x']
因为如果尚未设置db.collection.update({_id: '...'}, {$set:{'letters.0':'x'}});
字段,则上述操作的结果将为:
letters
有没有办法告诉mongo我的意图是创建一个数组,而不是一个对象?