一,sql 简单使用
use commentdb;
show dbs;
show collections;
db.comment.insert({_id:"1", content:"到底为啥出错了", userid:"1012", thumbup:2020});
db.comment.insert({_id:"2", content:"加班到半夜", userid:"1012", thumbup:1123});
db.comment.insert({_id:"3", content:"手机流量超了咋办", userid:"1012", thumbup:1023});
db.comment.insert({_id:"4", content:"坚持就是胜利", userid:"1012", thumbup:4532});
db.comment.find();
db.comment.find({thumbup:4532});
db.comment.findOne();
db.comment.find().limit(2);
db.comment.update({_id:"1"},{thumbup: 1000});
db.comment.update({_id:"2"},{$set:{thumbup: 1000}});
db.comment.remove();
db.comment.remove({thumbup: 1000});
db.comment.count();
db.comment.count({userid:"1012"});
db.comment.find({content:/流量/});
db.comment.find({content:/^加班/});
db.comment.find({_id:{$gt: "1"}});
db.comment.find({_id:{$gt: "1"}});
db.comment.find({_id:{$ne: "1"}});
db.comment.find({_id:{$gte: "1"}});
db.comment.find({_id:{$lte: "1"}});
db.comment.find( {$and:[ {_id:{$lte: "1"}}, {thumbup:{$gte: 10}} ] } );
db.comment.find( {$or:[ {_id:{$lte: "1"}}, {thumbup:{$gte: 10}} ] } );
db.comment.update({_id:"2"},{$inc:{thumbup:1}});
db.comment.find({userid:{$in:["1021", "1012"]}});
db.comment.find({userid:{$nin:["1021", "1012"]}});
二,mongodb-driver 简单使用
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
</dependency>
</dependencies>
public class MongoTest {
private MongoClient mongoClient = null;
private MongoCollection<Document> comment = null;
@Before
public void init()
{
this.mongoClient = new MongoClient("192.168.1.9");
MongoDatabase commentdb = mongoClient.getDatabase("commentdb");
this.comment = commentdb.getCollection("comment");
}
@After
public void colse()
{
this.mongoClient.close();
}
@Test
public void test()
{
FindIterable<Document> documents = comment.find();
for (Document document : documents) {
System.out.println("-------------------------");
System.out.println("_id:"+document.get("_id"));
System.out.println("name:"+document.get("name"));
}
}
@Test
public void test2()
{
Map<String, Object> conditionMap = new HashMap<>();
conditionMap.put("_id", "1");
BasicDBObject conditionObject = new BasicDBObject(conditionMap);
FindIterable<Document> documents = comment.find(conditionObject);
for (Document document : documents) {
System.out.println("-------------------------");
System.out.println("_id:"+document.get("_id"));
System.out.println("name:"+document.get("name"));
}
}
@Test
public void test3()
{
Map<String, Object> conditionMap = new HashMap<>();
conditionMap.put("_id", "8");
Document document = new Document(conditionMap);
comment.insertOne(document);
}
@Test
public void test4()
{
Map<String, Object> conditionMap = new HashMap<>();
conditionMap.put("_id", "1");
BasicDBObject conditionObject = new BasicDBObject(conditionMap);
Map<String, Object> contentMap = new HashMap<>();
contentMap.put("content", "mongo-test");
BasicDBObject contentObject = new BasicDBObject("$set", contentMap);
comment.updateOne(conditionObject, contentObject);
}
@Test
public void test5()
{
Map<String, Object> conditionMap = new HashMap<>();
conditionMap.put("_id", "1");
BasicDBObject object = new BasicDBObject(conditionMap);
comment.deleteOne(object);
}
}