搜索

首页  >  问答  >  正文

mongoDB批量插入文档时,运行下面代码,用MongoVUE查看数据库,mongo库中只存在一个文档?不应该是20个吗?

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

<code>import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.DBObject;

import com.mongodb.Mongo;

 

public class HelloMongoDB {

 

    private static Mongo mg;

    private static DB db;

    private static DBCollection col;

 

    public static void main(String[] args) throws UnknownHostException {

 

        HelloMongoDB hello = new HelloMongoDB();

 

        if (mg == null) {

            mg = new Mongo("127.0.0.1", 27017);

        }

 

        if (db == null) {

            db = mg.getDB("Studing");

        }

 

        if (col == null) {

            col = db.getCollection("test");

        }

 

        hello.InsertDocument(col);

 

    }

 

    public void InsertDocument(DBCollection col) {

 

        for (int i = 0; i < 10; i++) {

            BasicDBObject docu = new BasicDBObject();

            docu.put("type", "super");

            docu.put("price", i + 10);

            col.insert(docu);

        }

 

        for (int i = 0; i < 10; i++) {

            BasicDBObject docu = new BasicDBObject();

            docu.put("type", "tang");

            docu.put("price", i + 10);

            col.insert(docu);

 

        }

 

    }

 

}

</code>

世界只因有你世界只因有你2811 天前739

全部回复(2)我来回复

  • PHPz

    PHPz2017-04-24 16:01:34

    不好意思。。我实际测试了一下。。好像没有问题啊。。

    回复
    0
  • PHP中文网

    PHP中文网2017-04-24 16:01:34

    看起来没什么问题,调用getLastError看看有内容吗

    回复
    0
  • 取消回复