时间:2021-07-01 10:21:17 帮助过:47人阅读
进入MongoDB目录并建立存放数据的目录
cd mongodb-linux-x86_64-rhel70-3.4.9
mkdir -p data/db
启动MongoDB
bin/mongod --dbpath data/db
使用mongo命令打开命令行
bin/mongo
> show dbs
> show collections
> show users
> use test
> db.help()
> db.user.help()
> db.user.save({userId:"zhangsan",name:"张三"})
> db.user.find()
> db.user.find().help()
> db.user.save({userId:"lisi",name:"王五"})
> db.user.update({userId:"lisi"}, {$set:{name:"李四"}}, false, true)
> db.user.remove({userId:"lisi"})
> db.user.find()
更多用法可以参考菜鸟教程
从GitHub下载驱动mongo-java-driver-3.5.0.jar
private static void display(FindIterable<Document> result) {
for (Document doc : result) {
logger.debug(doc.toJson());
}
}
private static void testMongoDB() throws Exception {
MongoClient client = new MongoClient("192.168.1.181", 27017);
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> col = db.getCollection("users");
Document user = new Document();
user.append("userId", "zhangsan");
user.append("name", "张三");
user.append("sex", "男");
col.insertOne(user);
display(col.find());
col.updateMany(Filters.eq("userId", "zhangsan"), new Document("$set", new Document("sex", "女")));
display(col.find());
col.deleteMany(Filters.eq("userId", "zhangsan"));
display(col.find());
client.close();
}
MongoDB安装(转)
标签:move https not 令行 insert set driver cep mmu