2010-07-26 65 views

回答

172

討厭回答我的問題,但我意識到,你可以這樣做:

BasicDBObject doc = new BasicDBObject("name", "Matt"); 
collection.insert(doc); 
ObjectId id = (ObjectId)doc.get("_id"); 
+3

回答你自己的問題是[鼓勵](http://stackoverflow.com/help/self-answer),不用擔心。 – 2014-04-13 10:16:24

+0

謝謝你。你知道如何使用彈簧數據mongodb做同樣的事嗎? – 2015-11-02 07:09:15

7

我不知道有關Java驅動程序,但爲後人中,GetLastError函數命令可以運行來獲得的_id一個寫,甚至一個upsert(從1.5.4開始)

1

將文檔插入到MongoDB集合後,成功插入應更新必填字段(即_id)。您可以查詢_id的插入對象。

7

它的安全做

doc.set("_id", new ObjectId()) 

如果你看一下驅動程序代碼

if (ensureID && id == null){ 
    id = ObjectId.get(); 
    jo.put("_id" , id);  
} 

public static ObjectId get(){ 
    return new ObjectId(); 
} 
+0

你是不是故意說'這樣做可以省事'或'做'安全嗎? – pd40 2012-10-11 00:07:31

+1

出於某種原因,在MongoDB 2.2.2(而不是之前的2.2.0版本)和Java驅動程序2.10.1中,答案中的代碼不起作用;在將對象插入到文檔中後,我似乎無法獲得其_id,即使MongoDB明顯自動生成ObjectId。但是,您手動創建ObjectId的解決方案確實可行,並感謝此選項! – 2012-12-04 08:39:30

+0

BasicDBObject doc = new BasicDBObject("_id", new ObjectId()); System.out.println("doc.id before: " + doc.get("_id")); new Mongo("localhost").getDB("test").getCollection("t").insert(doc); System.out.println("doc.id after: " + doc.get("_id")); 此代碼適用於我,在新版本上測試mongo 2.2.2,驅動程序2.10.1 – zlob 2013-01-16 14:21:17

-2

這是插入操作:

DBCollection table1 = db.getCollection("Collection name"); 
BasicDBObject document = new BasicDBObject(); 
document.put("_id",value);  
document.put("Name", name); 
table1.insert(document); 

插入u得到最後插入編號:

DBCollection tableDetails = db.getCollection("collection name"); 
BasicDBObject queryDetails = new BasicDBObject(); 
queryDetails.put("_id", value); 
DBCursor cursorDetails =tableDetails.find(queryDetails); 
DBObject oneDetails; 
oneDetails=cursorDetails.next();   
String data=oneDetails.get("_id").toString(); 
System.out.println(data); 

得到值後轉換爲inter類型。

6

爲了避免從鑄造到ObjectObjectId,給予com.mongodb.client.MongoCollection collectionorg.bson.Document doc,你可以做到以下幾點:

collection.insert(doc); 
ObjectId id = doc.getObjectId("_id"); 
0

在MongoTemplate.class有一個方法

protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) { 

    assertUpdateableIdIfNotSet(objectToSave); 

    initializeVersionProperty(objectToSave); 

    maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName)); 

    DBObject dbDoc = toDbObject(objectToSave, writer); 

    maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName)); 
    Object id = insertDBObject(collectionName, dbDoc, objectToSave.getClass()); 

    populateIdIfNecessary(objectToSave, id); 
    maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc, collectionName)); 
} 

和方法爲我們設置ID

protected void populateIdIfNecessary(Object savedObject, Object id) { 

    if (id == null) { 
     return; 
    } 

    if (savedObject instanceof BasicDBObject) { 
     DBObject dbObject = (DBObject) savedObject; 
     dbObject.put(ID_FIELD, id); 
     return; 
    } 

    MongoPersistentProperty idProp = getIdPropertyFor(savedObject.getClass()); 

    if (idProp == null) { 
     return; 
    } 

    ConversionService conversionService = mongoConverter.getConversionService(); 
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(savedObject.getClass()); 
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject); 

    if (accessor.getProperty(idProp) != null) { 
     return; 
    } 

    new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProp, id); 
} 

我們可以看到實體是否是BasicDBObject的子類,它會爲我們設置一個id。

相關問題