如何使用mongoDb java异步驱动程序在插入mongoDb集合后获取_id
如何使用mongoDb java异步驱动程序在插入mongoDb集合后获取_id如何使用mongoDb java异步驱动程序在插入mongoDb集合后获取_id,java,mongodb,asynchronous,save,driver,Java,Mongodb,Asynchronous,Save,Driver,如何使用mongoDb java异步驱动程序在插入mongoDb集合后获取_id package test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.allanbank.mongodb.MongoClient; import com.allanbank.mongodb.MongoClientConfiguration; import com.
package test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.allanbank.mongodb.MongoClient;
import com.allanbank.mongodb.MongoClientConfiguration;
import com.allanbank.mongodb.MongoCollection;
import com.allanbank.mongodb.MongoDatabase;
import com.allanbank.mongodb.MongoFactory;
import com.allanbank.mongodb.bson.Document;
import com.allanbank.mongodb.bson.builder.BuilderFactory;
import com.allanbank.mongodb.bson.builder.DocumentBuilder;
import com.allanbank.mongodb.builder.Aggregate;
import com.xxxx.dto.FeedMongoDTO;
/**
* @author abhi
*
*/
public class MongoTestService {
public static transient Log log = LogFactory.getLog(FeedMongoOperations.class);
private MongoClient mongo;
private MongoDatabase db;
private MongoCollection collection;
public boolean openDbConnection() {
try {
MongoClientConfiguration config = new MongoClientConfiguration();
config.addServer("localhost:27017");
config.setMaxConnectionCount(10);
mongo = MongoFactory.createClient(config);
db = mongo.getDatabase("feedDatabase");
return true;
} catch (Exception e) {
return false;
}
}
public boolean closeDbConnection() {
try {
mongo.close();
return true;
} catch (Exception e) {
return false;
}
}
public String save(FeedMongoDTO feed, String collectionName) {
try {
collection = db.getCollection(collectionName);
DocumentBuilder b = BuilderFactory.start();
Document d1 = b.add("url", feed.getUrl()).addLong("mongoTimeStamp", feed.getMongoTimestamp())
.add("feedJsonArray", feed.getFeedJsonArray()).build();
collection.insert(d1);
return d1.get("id").toString();
} catch (Exception ex) {
return null;
}
}
public FeedMongoDTO getFeed(String mongoId, String collectionName) {
FeedMongoDTO feedMongoDTO = null;
try {
return feedMongoDTO;
} catch (Exception ex) {
return null;
}
}
}
其中FeedMongoDTO
的结构如下所示
public class FeedMongoDTO {
private String id;
private String url;
private Long mongoTimeStamp;
private JSONArray feedJsonArray;
// Getters
public String getId() {
return id;
}
public String getUrl() {
return url;
}
public Long getMongoTimestamp() {
return mongoTimeStamp;
}
public JSONArray getFeedJsonArray() {
return feedJsonArray;
}
// Setters
public void setId(String id) {
this.id = id;
}
public void setUrl(String url) {
this.url = url;
}
public void setMongoTimestamp(Long mongoTimestamp) {
this.mongoTimeStamp = mongoTimestamp;
}
public void setFeedJsonArray(JSONArray feedJsonArray) {
this.feedJsonArray = feedJsonArray;
}
}
我需要获取_id的值,但这里是d1.get(“id”)。toString()
导致NullPointerException
还有一件事我很困惑,我是否正确地使用了Save()方法。使用普通的mongodb驱动程序,这非常简单
public String save(FeedMongoDTO feed, String collectionName) {
try {
mongoTemplate.save(feed, collectionName);
return feed.getId();
} catch (Exception ex) {
return null;
}
}
提前谢谢
Abhilash:)在我看来,异步java驱动程序提供了进行同步查询的方法,例如使用普通调用。这对你的需要有意义吗?如果这是你需要经常做的事情,为什么不自己设置id呢。您可以使用0-arg构造函数轻松地构造一个新的构造函数,只需在插入之前将其添加到文档中即可
ObjectId id = new ObjectId();
documentBuilder.add("_id", id);
collection.insert(documentBuilder);
仅仅为了检索id而运行单独的查询似乎很愚蠢。在使用异步节点应用程序在我的目录中创建新名称时,我自己遇到了这个问题。我想通过ID将用户带到新创建的名称,而不将用户返回目录列表。所有的20个问题都是这样的,虽然对Node来说并不完全正确,但这让我走上了这条道路:
addName: function(nameDB, newName, callback){
nameID = new require('mongodb').ObjectID();
nameDB.collection('nameList').insert({"_id" : nameID, 'Name' : newName, 'type' : '', 'active' : 'no', 'modifiedDate' : ''}, function(err, result) {
if (!err) {
// success
callback(null, nameID);
} else {
// error
callback('error', err);
}
});
}
然后,我会从我的应用程序调用该函数:
mongo.addName(nameDB, newName, function(err, result) {
if (!err){
// success
// direct the user to the proper page with this new ObjectID found in the var 'result'
} else {
// error
console.log('There was an error adding the name: '+ result);
}
});
嗨,shelman,实际上有没有什么简单的方法可以在插入后获得_id,如前面讨论的代码示例中所示,其中id是在不调用任何新方法的情况下获得的,如
findOne
。谢谢:)你试过orid的答案了吗?在我看来,您必须使用insert调用的同步版本,因为异步版本不能以您想要的方式返回值。是的,我尝试了,它正在工作。谢谢你的建议,我会试试:)嗨,谢尔曼,我认为这是一个同步方法,因为有一个insertAsync()方法具有相同的方法签名。实际上,这是驱动程序中文档不是不可变的地方。“_id”字段有特殊处理,既可以快速检测没有id的文档,也可以确保即使在异步情况下调用方也可以访问注入的ObjectId.trycollection.insert(d1,耐久性.ACK)代码>你好,谢谢你的回复,但是当我按照你说的做时,我得到了一个错误类型MongoCollection中的方法insert(耐久性,DocumentAssignable…)不适用于参数(Document,耐久性)。更改为insertAsync()
当我按照那里说的那样做时,我得到:类型MongoCollection中的insertAsync(耐久性,DocumentAssignable…)方法不适用于参数(文档,耐久性)更改为insert()
我的错误,请翻转顺序collection.insert(耐久性.ACK,d1)代码>@abhips:正如您所发现的,如果还没有具有ObjectId的字段,\ u id是objectedelement类型的元素,则在执行插入操作时,驱动程序会在文档中插入一个带有ObjectId的'\u id'字段。该元素的值是一个ObjectId,您正在对其调用toString,并给出ObjectId('5150c3da57199014e86bedc6')。如果改为调用toHexString(),则将得到5150c3da57199014e86bedc6部件。类似于:d1.get(objectdelement.class,“_id”).toHexString();