win*_*ama 3 java mongodb nosql
我有一个问题,想知道如何通过MongoDB中的_id删除文档.我可以通过其他属性删除没问题但我似乎缺少正确的语法删除_id.
我的文档格式如下:
{ "_id" : { "$oid" : "57a49c6c33b10927ff09623e" }, "name" : "Brad" }
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的Java代码:
// Boiler plate
MongoClient client = new MongoClient( "localhost" , 27017 );
MongoDatabase db = client.getDatabase("my-database");
MongoCollection<Document> collection = db.getCollection("my-collection")
// This works
collection.deleteOne(new Document("name", "Brad"));
// This does not work
collection.deleteOne(new Document("_id", "57a49c6c33b10927ff09623e"));
Run Code Online (Sandbox Code Playgroud)
任何人都知道我哪里出错了?
Field _id有类型ObjectId,并且"57a49c6c33b10927ff09623e"类型为String.
而是试试
collection.deleteOne(new Document("_id", new ObjectId("57a49c6c33b10927ff09623e")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7378 次 |
| 最近记录: |