Swift MongoKitten:检查ObjectId是否存在
如何检查集合中是否存在ObjectId? 我从mongo控制台执行以下操作:Swift MongoKitten:检查ObjectId是否存在,swift,mongokitten,Swift,Mongokitten,如何检查集合中是否存在ObjectId? 我从mongo控制台执行以下操作: > db.collection.find({_id: ObjectId("5bb216a8475fe42644993a40")}, {_id: 1}).limit(1); 如何使用MongoKitten实现同样的功能 let mongoClient = try MongoKitten.Database("mongodb://\(username):\(password)@\(ipAddress)/\(datab
> db.collection.find({_id: ObjectId("5bb216a8475fe42644993a40")}, {_id: 1}).limit(1);
如何使用MongoKitten实现同样的功能
let mongoClient = try MongoKitten.Database("mongodb://\(username):\(password)@\(ipAddress)/\(database)")
let myCollection = mongoClient[collection]
let oid = try ObjectId("5bb216a8475fe42644993a41")
let document = try myCollection.findOne("_id" == oid)
如果ObjectId不存在,文档
将为nil
另外,基于文档,Mongo DB的findOne()
方法比find()
方法占用的资源要多得多。但是在MongoKitten的引擎盖下findOne()
使用find()
方法: