java直接操作mongodb语句
参考如下public class MongoDBJDBC { public static void main(String[] args) { try { // 实例化Mongo对象,连接27017端口 Mongo mongo = new Mongo(“localhost”, 27017); // 连接名为yourdb的数据库,假如数据库不存在的话,mongodb会自动建立 DB db = *(“test”); // Get collection from MongoDB, database named “yourDB” // 从Mongodb中获得名为yourColleection的数据集合,如果该数据集合不存在,Mongodb会为其新建立 DBCollection collection = *lection(“test1”); // 使用BasicDBObject对象创建一个mongodb的document,并给予赋值。
BasicDBObject document = new BasicDBObject(); //*(“id”, 1001); //*(“msg”, “hello world mongoDB in Java”); // 将新建立的document保存到collection中去 //*(document); // 创建要查询的document BasicDBObject searchQuery = new BasicDBObject(); *(“name”, “chen”); // 使用collection的find方法查找document DBCursor cursor = *(searchQuery); // 循环输出结果 while (*t()) { *n(*()); } *n(“Hello World”); } catch (UnknownHostException e) { *tackTrace(); } catch (MongoException e) { *tackTrace(); } }}。
mongodb 分组的语句怎么写
/ 首先利$match筛选出where条件 此处看你的查询范围条件了BasicDBObject[] array = {new BasicDBObject(“startTimeLong”, new BasicDBObject(“$gte”,beginTime)), new BasicDBObject(“startTimeLong”, new BasicDBObject(“$lt”,endTime)) }; BasicDBObject cond = new BasicDBObject(); *(“$and”, array); DBObject match = new BasicDBObject(“$match”, cond); // 利用$project拼装group需要的数据,包含name列、age列 DBObject fields = new BasicDBObject(“name”, 1); *(“age”, 1); DBObject project = new BasicDBObject(“$project”, fields); // 利用$group进行分组 DBObject _group = new BasicDBObject(“name”, “$name”);_*(“age”, “$age”);DBObject groupFields = new BasicDBObject(“_id”, _group); //总数*(“count”, new BasicDBObject(“$sum”, 1));DBObject group = new BasicDBObject(“$group”, groupFields); AggregationOutput output = *InfoCollection().aggregate(match, project, group);。
mongodb语句可以写成批处理吗
在MongoDB中,文档是对数据的抽象,它被使用在Client端和Server端的交互中。所有的Client端(各种语言的Driver)都会使用这种抽象,它的表现形式就是我们常说的BSON(Binary JSON )。
BSON是一个轻量级的二进制数据格式。
MongoDB能够使用BSON,并将BSON作为数据的存储存放在磁盘中。
当Client端要将写入文档,使用查询等等操作时,需要将文档编码为BSON格式,然后再发送给Server端。同样,Server端的返回结果也是编码为BSON格式再放回给Client端的。
java直接操作mongodb语句
参考如下
public class MongoDBJDBC {
public static void main(String[] args) {
try {
// 实例化Mongo对象,连接27017端口
Mongo mongo = new Mongo(“localhost”, 27017);
// 连接名为yourdb的数据库,假如数据库不存在的话,mongodb会自动建立
DB db = *(“test”);
// Get collection from MongoDB, database named “yourDB”
// 从Mongodb中获得名为yourColleection的数据集合,如果该数据集合不存在,Mongodb会为其新建立
DBCollection collection = *lection(“test1”);
// 使用BasicDBObject对象创建一个mongodb的document,并给予赋值。
BasicDBObject document = new BasicDBObject();
//*(“id”, 1001);
//*(“msg”, “hello world mongoDB in Java”);
// 将新建立的document保存到collection中去
//*(document);
// 创建要查询的document
BasicDBObject searchQuery = new BasicDBObject();
*(“name”, “chen”);
// 使用collection的find方法查找document
DBCursor cursor = *(searchQuery);
// 循环输出结果
while (*t()) {
*n(*());
}
*n(“Hello World”);
} catch (UnknownHostException e) {
*tackTrace();
} catch (MongoException e) {
*tackTrace();
}
}
}