mongoDB批量插入文档时,运行下面代码,用MongoVUE查看数据库,mongo库中只存在一个文档?不应该是20个吗?
世界只因有你
世界只因有你 2017-04-24 15:59:34
0
2
704
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);

        }

    }

}
世界只因有你
世界只因有你

reply all(2)
PHPzhong

Sorry. . I actually tested it. . There seems to be no problem. .

洪涛

It seems there is no problem, call getLastError to see if there is any content

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template