本文整理汇总了Java中com.mongodb.DBCollection.insert方法的典型用法代码示例。如果您正苦于以下问题:Java DBCollection.insert方法的具体用法?Java DBCollection.insert怎么用?Java DBCollection.insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.DBCollection
的用法示例。
在下文中一共展示了DBCollection.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNextId
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public int getNextId(GridFS destDatabase) {
DBCollection countersCollection = destDatabase.getDB().getCollection("counters");
DBObject record = countersCollection.findOne(new BasicDBObject("_id", "package"));
if (record == null) {
BasicDBObject dbObject = new BasicDBObject("_id", "package");
dbObject.append("seq", 0);
countersCollection.insert(dbObject);
record = dbObject;
}
int oldID = (int) record.get("seq");
int newID = oldID + 1;
record.put("seq", newID);
countersCollection.update(new BasicDBObject("_id", "package"), record);
return newID;
}
示例2: InsertData
import com.mongodb.DBCollection; //导入方法依赖的package包/类
private int InsertData(SQLInsertStatement state) {
if (state.getValues().getValues().size() ==0 ){
throw new RuntimeException("number of columns error");
}
if (state.getValues().getValues().size() != state.getColumns().size()){
throw new RuntimeException("number of values and columns have to match");
}
SQLTableSource table=state.getTableSource();
BasicDBObject o = new BasicDBObject();
int i=0;
for(SQLExpr col : state.getColumns()) {
o.put(getFieldName2(col), getExpValue(state.getValues().getValues().get(i)));
i++;
}
DBCollection coll =this._db.getCollection(table.toString());
coll.insert(new DBObject[] { o });
return 1;
}
示例3: addUsers
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@ChangeSet(order = "01",
author = "developer",
id = "01-addUsers")
public void addUsers(final DB db) {
final DBCollection userCollection = db.getCollection(User.COLLECTION_NAME);
userCollection.insert(BasicDBObjectBuilder
.start()
.add(FIELD_NAME_ID, new ObjectId("590f86d92449343841cc2c3f"))
.add(User.FIELD_NAME_FIRST_NAME, "User")
.add(User.FIELD_NAME_LAST_NAME, "One")
.add(User.FIELD_NAME_EMAIL, "[email protected]")
.get());
userCollection.insert(BasicDBObjectBuilder
.start()
.add(FIELD_NAME_ID, new ObjectId("590f86d92449343841cc2c40"))
.add(User.FIELD_NAME_FIRST_NAME, "User")
.add(User.FIELD_NAME_LAST_NAME, "Two")
.add(User.FIELD_NAME_EMAIL, "[email protected]")
.get());
}
示例4: testFindOne
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void testFindOne() throws UnknownHostException {
MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print");
DBCollection collection = client.getCollection("marc");
BasicDBObject doc = createTestObject();
collection.insert(doc);
assertEquals(1, collection.count());
DBObject myDoc = collection.findOne();
assertEquals("MongoDB", myDoc.get("name"));
assertEquals("database", myDoc.get("type"));
assertEquals(1, myDoc.get("count"));
assertEquals(BasicDBObject.class, myDoc.get("info").getClass());
assertEquals(new BasicDBObject("x", 203).append("y", 102), myDoc.get("info"));
assertEquals(203, ((BasicDBObject)myDoc.get("info")).get("x"));
assertEquals(Integer.class, ((BasicDBObject)myDoc.get("info")).get("x").getClass());
System.out.println(myDoc);
collection.remove(new BasicDBObject("name", "MongoDB"));
}
示例5: testImport
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public synchronized void testImport() throws URISyntaxException, IOException, InterruptedException {
MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print");
DBCollection collection = client.getCollection("marc");
assertEquals(0, collection.count());
boolean insert = true;
if (insert) {
JsonPathCache<? extends XmlFieldInstance> cache;
List<String> records = FileUtils.readLines("general/marc.json");
for (String record : records) {
cache = new JsonPathCache<>(record);
Object jsonObject = jsonProvider.parse(record);
String id = cache.get("$.controlfield.[?(@.tag == '001')].content").get(0).getValue();
String x003 = cache.get("$.controlfield.[?(@.tag == '003')].content").get(0).getValue();
BasicDBObject doc = new BasicDBObject("type", "marcjson")
.append("id", id)
.append("x003", x003)
.append("record", record);
collection.insert(doc);
}
assertEquals(674, collection.count());
}
collection.remove(new BasicDBObject("type", "marcjson"));
assertEquals(0, collection.count());
}
示例6: insert
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public Object insert(DBCollection collection, WriteConcern writeConcern) {
DBObject document = new BasicDBObject();
// 匹配_id
for (int i = 0, n = columns.size(); i < n; i++) {
// document.put(columns.get(i), values.get(i).getValue());
String tempColumn = columns.get(i);
if (3 == tempColumn.length() && tempColumn.equals("_id")) {
document.put(tempColumn, new ObjectId(values.get(i).getValue().toString()));
} else {
document.put(tempColumn, values.get(i).getValue());
}
}
log(document);
// TODO: WriteConcern.ACKNOWLEDGED需要可以配置
// WriteResult result = collection.insert(document, WriteConcern.ACKNOWLEDGED);
// collection.insert(document, MongoComponent.getInstance().getDefaultWriteConcern());
collection.insert(document, writeConcern);
Object oid = document.get("_id");
if (null != oid) {
return oid.toString();
}
return null;
}
示例7: createDatabase
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public DB createDatabase(String databaseName) throws MongoServiceException {
try {
DB db = client.getDB(databaseName);
// save into a collection to force DB creation.
DBCollection col = db.createCollection("foo", null);
BasicDBObject obj = new BasicDBObject();
obj.put("foo", "bar");
col.insert(obj);
// drop the collection so the db is empty
// col.drop();
return db;
} catch (MongoException e) {
// try to clean up and fail
try {
deleteDatabase(databaseName);
} catch (MongoServiceException ignore) {}
throw handleException(e);
}
}
示例8: doPut
import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
* Perform a single 'put' operation on the local object store.
*
* @param ref Object reference string of the object to be written.
* @param obj JSON string encoding the object to be written.
* @param collection Collection to put to.
*
* @return a ResultDesc object describing the success or failure of the
* operation.
*/
private ResultDesc doPut(String ref, String obj, DBCollection collection,
boolean requireNew)
{
String failure = null;
if (obj == null) {
failure = "no object data given";
} else {
try {
DBObject objectToWrite = jsonLiteralToDBObject(obj, ref);
if (requireNew) {
WriteResult wr = collection.insert(objectToWrite);
} else {
DBObject query = new BasicDBObject();
query.put("ref", ref);
collection.update(query, objectToWrite, true, false);
}
} catch (Exception e) {
failure = e.getMessage();
}
}
return new ResultDesc(ref, failure);
}
示例9: execute
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@Override
protected Object execute(DBCollection collection, MongoHandle mongoHandle) throws DBException
{
try
{
collection.insert(dbObjects);
return true;
} catch (Exception e)
{
throw new DBException(e);
}
}
示例10: addAuthorities
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@ChangeSet(order = "01", author = "initiator", id = "01-addAuthorities")
public void addAuthorities(DB db) {
DBCollection authorityCollection = db.getCollection("jhi_authority");
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_ADMIN")
.get());
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_USER")
.get());
}
示例11: addUsers
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@ChangeSet(order = "02", author = "initiator", id = "02-addUsers")
public void addUsers(DB db) {
DBCollection usersCollection = db.getCollection("jhi_user");
usersCollection.createIndex("login");
usersCollection.createIndex("email");
usersCollection.insert(BasicDBObjectBuilder.start()
.add("_id", "user-0")
.add("login", "system")
.add("password", "*************")
.add("first_name", "")
.add("last_name", "System")
.add("email", "[email protected]")
.add("activated", "true")
.add("lang_key", "en")
.add("created_by", "system")
.add("created_date", new Date())
.add("authorities", authoritiesAdminAndUser)
.get()
);
usersCollection.insert(BasicDBObjectBuilder.start()
.add("_id", "user-1")
.add("login", "anonymousUser")
.add("password", "*************")
.add("first_name", "Anonymous")
.add("last_name", "User")
.add("email", "[email protected]")
.add("activated", "true")
.add("lang_key", "en")
.add("created_by", "system")
.add("created_date", new Date())
.add("authorities", new Map[]{})
.get()
);
}
示例12: addAuthorities2
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@ChangeSet(order = "04", author = "user", id = "04-addAuthorities-2")
public void addAuthorities2(DB db) {
DBCollection authorityCollection = db.getCollection("jhi_authority");
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_MANAGER")
.get());
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_SUPPORT")
.get());
}
示例13: addAuthorities3
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@ChangeSet(order = "05", author = "user", id = "05-addAuthorities-3")
public void addAuthorities3(DB db) {
DBCollection authorityCollection = db.getCollection("jhi_authority");
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_ACTUATOR")
.get());
}
示例14: testInsert
import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void testInsert() throws UnknownHostException {
MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print");
DBCollection collection = client.getCollection("marc");
BasicDBObject doc = createTestObject();
collection.insert(doc);
assertNotNull(collection);
assertEquals(1, collection.count());
collection.remove(new BasicDBObject("name", "MongoDB"));
assertEquals(0, collection.count());
}
示例15: onRootAccess
import com.mongodb.DBCollection; //导入方法依赖的package包/类
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody Result onRootAccess() {
DBCollection collection = mongoTemplate.getCollection("test");
long count = collection.getCount();
log.info("Object count in 'test' collection before insert: " + count + "<br/> Inserting one object.<br/>");
BasicDBObject dBObject = new BasicDBObject();
dBObject.put("hello", "world");
collection.insert(dBObject);
count = collection.count();
log.info("Object count in test collection after insert:" + count);
Result result = new Result();
List<DBObject> dbObjects = new ArrayList<DBObject>();
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
com.mongodb.DBObject obj = cursor.next();
final String value = (String) obj.get("hello");
DBObject object = new DBObject();
object.setKey("hello");
object.setValue(value);
dbObjects.add(object);
}
result.setDbObjects(dbObjects);
result.setStatus(
"Successfully accessed Mongodb service. Retrieving the data object inserted in test collection.");
collection.drop();
return result;
}