我正在创建一个java应用程序,我想在我的mongoDB中插入一些数据,但我想用我的属性替换默认id
这是我的代码:
MongoClient mongoClient = new MongoClient("localhost");
DB db = mongoClient.getDB("indexdb");
DBCollection coll = db.getCollection("indexCollection");
for (Map.Entry<String, ReduceResult> rr : directindex.entrySet()) {
BasicDBObject doc = new BasicDBObject("DOC", rr.getKey() );
Map<String,Integer>wordmap=new HashMap<String,Integer>();
for (Map.Entry<String, Integer> r : rr.getValue().getIndexedFile().entrySet()) {
wordmap.put(r.getKey(), r.getValue());
}
doc.append("terms", wordmap);
coll.insert(doc);
}
输出
{ "_id" : { "$oid" : "58e5889ce4b02461ad5af080"} , "DOC" : "/home/ion/Desktop/RIW/input/doc4" , "terms" : { "dan" : 1 , "gutui" : 1}}
{ "_id" : { "$oid" : "58e5889ce4b02461ad5af081"} , "DOC" : "/home/ion/Desktop/RIW/input/doc3" , "terms" : { "bune" : 1 , "gutui" : 1}}
{ "_id" : { "$oid" : "58e5889ce4b02461ad5af082"} , "DOC" : "/home/ion/Desktop/RIW/input/doc2" , "terms" : { "dan" : 1 , "mere" : 2}}
{ "_id" : { "$oid" : "58e5889ce4b02461ad5af083"} , "DOC" : "/home/ion/Desktop/RIW/input/doc1" , "terms" : { "ana" : 1 , "mere" : 1 , "pere" : 1}}
但是我想插入没有那个&#34; _id&#34;我希望我的对象的id是&#34; DOC&#34; 谁能帮我?谢谢!
答案 0 :(得分:0)
您可以创建自己的字段并将其命名为“id”。在那里,你将存储你想要的ID。
在您的集合的架构中,您可以告诉Mongo不要像这样创建自动ID:
// disabled id
var schema = new Schema({ name: String }, { id: false });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p.id); // undefined