怎样查看保存到mongobd的文件_Java 保存到MongoDb的文档的几种方法

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似 json的bjson格式,因此可以存储比较复杂的数据类型。

import java.net.UnknownHostException;

import java.util.HashMap;

import java.util.Map;

import com.mongodb.BasicDBObject;

import com.mongodb.BasicDBObjectBuilder;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.DBObject;

import com.mongodb.Mongo;

import com.mongodb.MongoException;

import com.mongodb.util.JSON;

/**

* Java MongoDB : Insert a Document

*

*/

public class InsertDocumentApp {

public static void main(String[] args) {

try {

Mongo mongo = new Mongo("localhost", 27017);

DB db = mongo.getDB("yourdb");

// get a single collection

DBCollection collection = db.getCollection("dummyColl");

// BasicDBObject example

System.out.println("BasicDBObject example...");

BasicDBObject document = new BasicDBObject();

document.put("database", "mkyongDB");

document.put("table", "hosting");

BasicDBObject documentDetail = new BasicDBObject();

documentDetail.put("records", "99");

documentDetail.put("index", "vps_index1");

documentDetail.put("active", "true");

document.put("detail", documentDetail);

collection.insert(document);

DBCursor cursorDoc = collection.find();

while (cursorDoc.hasNext()) {

System.out.println(cursorDoc.next());

}

collection.remove(new BasicDBObject());

// BasicDBObjectBuilder example

System.out.println("BasicDBObjectBuilder example...");

BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()

.add("database", "mkyongDB")

.add("table", "hosting");

BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()

.add("records", "99")

.add("index", "vps_index1")

.add("active", "true");

documentBuilder.add("detail", documentBuilderDetail.get());

collection.insert(documentBuilder.get());

DBCursor cursorDocBuilder = collection.find();

while (cursorDocBuilder.hasNext()) {

System.out.println(cursorDocBuilder.next());

}

collection.remove(new BasicDBObject());

// Map example

System.out.println("Map example...");

Map documentMap = new HashMap();

documentMap.put("database", "mkyongDB");

documentMap.put("table", "hosting");

Map documentMapDetail = new HashMap();

documentMapDetail.put("records", "99");

documentMapDetail.put("index", "vps_index1");

documentMapDetail.put("active", "true");

documentMap.put("detail", documentMapDetail);

collection.insert(new BasicDBObject(documentMap));

DBCursor cursorDocMap = collection.find();

while (cursorDocMap.hasNext()) {

System.out.println(cursorDocMap.next());

}

collection.remove(new BasicDBObject());

// JSON parse example

System.out.println("JSON parse example...");

String json = "{'database' : 'mkyongDB','table' : 'hosting'," +

"'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";

DBObject dbObject = (DBObject)JSON.parse(json);

collection.insert(dbObject);

DBCursor cursorDocJSON = collection.find();

while (cursorDocJSON.hasNext()) {

System.out.println(cursorDocJSON.next());

}

collection.remove(new BasicDBObject());

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (MongoException e) {

e.printStackTrace();

}

}

}

MongoDB 聚合框架提供了一种数据处理方式,它能够对集合中的文档进行分组、筛选、投影、排序、限制以及多表关联等操作,从而生成新的文档集合。下面是几种 MongoDB 聚合查询方式的详解: 1. $match 查询 $match 查询通过筛选文档中的字段来过滤数据,类似于 SQL 中的 WHERE 子句。可以使用各种比较运算符、逻辑运算符和正则表达式等条件来实现高级查询。例如,以下代码将返回所有 age 大于等于 18 的文档: ``` db.collection.aggregate([ { $match : { age : { $gte : 18 } } } ]) ``` 2. $group 查询 $group 查询通过将文档分组来聚合数据。可以使用 $sum、$avg、$min、$max 等聚合运算符来计算每个分组的结果。例如,以下代码将返回每个国家的总人口数: ``` db.collection.aggregate([ { $group : { _id : "$country", population: { $sum : "$population" } } } ]) ``` 3. $project 查询 $project 查询用于投影文档中的字段,类似于 SQL 中的 SELECT 子句。可以使用 $addFields、$subtract、$multiply、$divide 等运算符来进行计算或添加新的字段。例如,以下代码将返回包含 name 和 age 字段的文档: ``` db.collection.aggregate([ { $project : { name : 1, age : 1 } } ]) ``` 4. $sort 查询 $sort 查询用于对文档进行排序,类似于 SQL 中的 ORDER BY 子句。可以使用 1 或 -1 来指定升序或降序排列。例如,以下代码将按 age 字段降序排列文档: ``` db.collection.aggregate([ { $sort : { age : -1 } } ]) ``` 以上是 MongoDB 聚合查询的几种方式,它们可以组合使用来实现更复杂的查询。在 Java 中,可以使用 MongoDBJava 驱动程序来进行聚合查询。具体的实现方式可以参考 MongoDB 的官方文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值