2013-12-17 32 views
0

我可以从MongoDB中的集合中检索单个条目,但我无法检索整个集合。在我检索完整集合后,我想将其放入字典中。检索整个集合应该很简单。我测试了我在shell中用来完成任务的各种命令,但是一旦转换为objective-c,所有命令都不成功。检索整个集合,然后将其添加到字典

//Message Retrieval 
    BSONDocument *resultDoc = [collection findAllWithError:&error]; 
    NSDictionary *result = [BSONDecoder decodeDictionaryWithDocument:resultDoc]; 
    NSLog(@"fetch result: %@", result); 
+0

@noa我了解如何获取和'findOneWithPredicate只有一个条目:predicate',但是有什么办法只是做一个collection.find( ),以便它返回集合下的所有项目?我已经浏览了ObjCMongodb的功能,并没有引起我的注意,它能够做到这一点。 – Nate

+1

是的,它是'-findAllWithError:'。 – paulmelnikow

+0

@noa尝试使用此(上述更新代码) – Nate

回答

1

-findAllWithError:返回文档的数组:

NSArray *results = [collection findAllWithError:&error]; 
for (BSONDocument *resultDoc in results) { 
    NSDictionary *result = [BSONDecoder decodeDictionaryWithDocument:resultDoc]; 
    NSLog(@"fetch result: %@", result); 
} 
+0

非常感谢您的帮助和快速响应 – Nate

相关问题