Java operation MongoDB CRUD simple operation

I downloaded mongo-2.8.0.jar — Version 2.8.0
Open the mongo shell — create a new database test –( use test)
Open the new eclipse project and import junit, mongo-2.8.0.jar.
Create a new test class as follows:
package com.db;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.junit.After;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class DBUtils {
private static Mongo mongo;
private static DB db;
static {
try {
mOngo= new
Mongo(“localhost”, 27017);
} catch (UnknownHostException
e) {
e.printStackTrace();
} catch (MongoException e)
{
e.printStackTrace();
}
db = mongo. getDB(“test”);
}

@After
public void output() {
// After testing, display all
DBCollection dbc =
db. getCollection(“user”);
DBCursor cursor =
dbc.find();
while (cursor. hasNext())
{
//
cursor.next is a line of records (a document)
System.out.println(cursor.next());
}
}

@Test
public void testInsert() throws Exception {
DBCollection dbc =
db. getCollection(“user”);
DBObject object = new
BasicDBObject();
object.put(“username”,
“zdw”);
object.put(“password”,
“123”);
// A person has a position
//
Database json format: {username:”zdw”,password:”123″,position:{name:”engineer”}}
BasicDBObject o = new
BasicDBObject();
o.put(“name”, “manager”);
object.put(“position”,
o);
dbc.insert(object);

object = new
BasicDBObject();
object.put(“username”,
“zhangjun”);
object.put(“password”,
“456”);
// A person has multiple positions (multiple documents in one document)
//
Database json format: {username:”zhangjun”,password:”456″,position:[{name:”engineer”},{name:”designer”}]}
List list = new
ArrayList();
o = new BasicDBObject();
o.put(“name”, “engineer”);
list.add(o);
o = new BasicDBObject();
o.put(“name”, “designer”);
list.add(o);
object.put(“position”,
list);
dbc.insert(object);
}

// Delete specified
@Test
public void testRemoveOne() throws Exception {
DBCollection dbc =
db. getCollection(“user”);
DBObject o = new
BasicDBObject();
o. put(“username”,
“zhangjun”);
dbc. remove(o);
}

@Test
public void testModify() throws Exception {
DBCollection dbc =
db. getCollection(“user”);
//
To modify the password according to the id, you need to add $set, otherwise the password of all records will be modified, and other fields will be deleted.
int result = dbc.update(
new
BasicDBObject(“_id”, new ObjectId(
“4ff8eaa65dbc67340278a07c”)),
new
BasicDBObject(“$set”, new BasicDBObject(“password”,
“4444”))).getN();
System.out.println(result);
}

@Testhttp://www.shengshiyouxi.com
public void testRemove() throws Exception {
DBCollection dbc =
db. getCollection(“user”);
// delete all
dbc.drop();
}

// Query specification
@Test
public void testFindOne() throws Exception {
DBCollection dbc =
db. getCollection(“user”);
DBObject o = new
BasicDBObject();
o.put(“username”, “zdw”);
DBObject obj =
dbc.findOne(o);
System.out.println(obj);
}

}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/java-operation-mongodb-crud-simple-operation/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索