好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解

MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,Mongo有个内置的连接池(池大小默认为10个)。

下面代码给大家介绍MongoDB的java驱动操作,具体代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Pattern;

import org.bson.Document;

import com.mongodb.MongoClient;

import com.mongodb.MongoCredential;

import com.mongodb.ServerAddress;

import com.mongodb.client.FindIterable;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

public class MongoZyh {

public static void main(String[] args) {

try {

// 连接到MongoDB服务,ServerAddress()两个参数分别为 服务器地址 和 端口

ServerAddress serverAddress = new ServerAddress( "localhost" , 27017 );

List<ServerAddress> addrs = new ArrayList<ServerAddress>();

addrs.add(serverAddress);

// 三个参数分别为 用户名 数据库名称 密码

MongoCredential credential = MongoCredential

.createScramSha1Credential( "zyh" , "admin" ,

"zyh" .toCharArray());

List<MongoCredential> credentials = new ArrayList<MongoCredential>();

credentials.add(credential);

// 通过连接认证获取MongoDB连接

MongoClient mongoClient = new MongoClient(addrs, credentials);

// 连接到数据库

MongoDatabase mongoDatabase = mongoClient.getDatabase( "zyhdb" );

// 新建集合,执行后会在数据库里新建一个空的集合

// mongoDatabase.createCollection("student");

// System.out.println("新建集合成功");

// 获取集合,并往集合中插入数据

MongoCollection<Document> mongoCollection = mongoDatabase

.getCollection( "student" );

// 插入一条数据

// Document document = new Document();

// document.append("name", "zhangsan");

// document.append("age", 28);

// mongoCollection.insertOne(document);

// System.out.println("插入一条数据成功");

// 插入多条数据

// List<Document> documentList = new ArrayList<Document>();

// Document document1 = new Document();

// document1.append("name", "lisi");

// document1.append("age", 28);

// document1.append("sex", "男");

// Document document2 = new Document();

// document2.append("name", "wangwu");

// document2.append("age", 31);

// document2.append("sex", "男");

// documentList.add(document1);

// documentList.add(document2);

// mongoCollection.insertMany(documentList);

// System.out.println("插入多条数据成功");

// 查询数据

// 查询集合中所有的数据

// FindIterable<Document> findIterable = mongoCollection.find();

// MongoCursor<Document> mongoCursor = findIterable.iterator();

// while (mongoCursor.hasNext()) {

// System.out.println(mongoCursor.next());

// }

// 根据条件查询

// Document query = new Document();

// query.put("age", new Document("$lt", 30));

// query.put("sex", "男");

// query.put("name", query);

// 正则表达式查询

// Pattern pattern = Pattern.compile("^zhang");

// query.put("name", pattern);

// 排序

// Document sort = new Document();

// sort.put("name", -1); // 1是正序,-1是倒序

// FindIterable<Document> findIterable = mongoCollection.find(query)

// .sort(sort);

// MongoCursor<Document> mongoCursor = findIterable.iterator();

// while (mongoCursor.hasNext()) {

// Document doc = mongoCursor.next();

// System.out.print("name:" + doc.get("name") + "...");

// System.out.print("age:" + doc.get("age") + "...");

// System.out.println("sex:" + doc.get("sex") + "...");

// }

// mongoCollection.findOneAndUpdate(查询条件, 修改内容); // 查询出第一条数据并修改

// mongoCollection.findOneAndDelete(查询条件); // 查询出第一条数据并删除

// mongoCollection.findOneAndReplace(查询条件, 替换内容); // 查询出第一条数据并替换

// 修改数据

// Document query = new Document();

// query.put("age", 28);

// Document update = new Document();

// Document d = new Document();

// d.put("birthday", new Date());

// d.put("name", "zhangsan");

// update.put("$set", d);

// mongoCollection.updateOne(query, update); // 修改查询到的第一条数据

// mongoCollection.updateMany(查询条件, 修改内容);// 修改查询到的所有数据

// 删除数据

// Document query = new Document();

// query.put("age", 28);

// mongoCollection.deleteOne(query); // 删除查询到的第一条数据

// mongoCollection.deleteMany(查询条件); // 删除查询到的所有数据

// mongoCollection.drop(); // 删除集合

} catch (Exception e) {

e.printStackTrace();

}

}

}

关于小编给大家介绍的MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解就给大家介绍这么多,希望对大家有所帮助,还会给大家持续更新MongoDB相关知识,敬请关注服务器之家网站!

查看更多关于MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解的详细内容...

  阅读:22次