1. MongoDB下的高级查询示例
- [root@localhost ~]# mongo
- MongoDB shell version: 1.8.1
- connecting to: test
- > db
- test
- > show collections
- data_test
- system.indexes
- system.users
- > db.data_test.find().skip(3).limit(4)//分页查询,从第4条记录起,每页4条。
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",
- "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",
- "other" : "nothing5" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",
- "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",
- "other" : "nothing7" } }
- > db.data_test.find({},{},4,3)//与上相同,注意此页大小和起始位置的位置
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",
- "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",
- "other" : "nothing5" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",
- "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",
- "other" : "nothing7" } }
- > db.data_test.find().sort({"userName":-1})//order by:按userName倒序
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf5c"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "interests" : { "game" : "game9", "ball" : "ball9",
- "other" : "nothing9" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf5b"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "interests" : { "game" : "game8", "ball" : "ball8",
- "other" : "nothing8" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",
- "other" : "nothing7" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",
- "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",
- "other" : "nothing5" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",
- "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf56"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "interests" : { "game" : "game3", "ball" : "ball3",
- "other" : "nothing3" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf55"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "interests" : { "game" : "game2", "ball" : "ball2",
- "other" : "nothing2" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf5d"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "interests" : { "game" : "game10", "ball" :
- "ball10", "other" : "nothing10" } }
- { "_id" : ObjectId("4dd7c914b2d5f68db79cdf54"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "interests" : { "game" : "game1", "ball" : "ball1",
- "other" : "nothing1" } }
- > db.data_test.find({"userName":{$ne:"Bill Tu10"},"age":{$gt:7}})//查询userName!='Bill Tu10' and age>7
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4128"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "interests" : { "game" : "game8", "ball" :
- "ball8", "other" : "nothing8" } }
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4129"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "interests" : { "game" : "game9", "ball" :
- "ball9", "other" : "nothing9" } }
- > db.data_test.find({"age":{$gte:2},"age":{$lte:5}})//查询age>=2 and age<=5
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4121"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "interests" : { "game" : "game1", "ball" :
- "ball1", "other" : "nothing1" } }
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4122"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "interests" : { "game" : "game2", "ball" :
- "ball2", "other" : "nothing2" } }
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4123"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "interests" : { "game" : "game3", "ball" :
- "ball3", "other" : "nothing3" } }
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4124"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "interests" : { "game" : "game4", "ball" :
- "ball4", "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7cf07b2d5f535b69b4125"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "interests" : { "game" : "game5", "ball" :
- "ball5", "other" : "nothing5" } }
- > db.data_test.find({"rank":{$all:[7,7]}})//查询rank=all(7,7)
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
- "game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
- > db.data_test.find({"rank":{$all:[7,7,7]}})//查询rank=all(7,7,7)
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
- "game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
- > db.data_test.find({"userName":{$exists:false}})
- > db.data_test.find({"age":{$mod:[2,0]}})//查询age%2==0
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
- "game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
- "game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
- "game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
- "game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
- { "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
- > db.data_test.find({"rank":{$in:[3,4]}})//查询rank in(3,4)
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
- "game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
- "game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
- > db.data_test.find({"age":{$nin:[2,3]}})//查询rank not in(2,3)
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
- "game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
- "game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
- "game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
- "game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
- "game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
- "game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
- "game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
- { "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
- > db.data_test.find({$or:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询age>3 or rank=all(1,1)
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
- "game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
- "game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
- "game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
- "game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
- "game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
- "game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
- "game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
- { "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
- > db.data_test.find({$nor:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询not (age>3 or rank=all(1,1))
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
- "game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
- "game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
- > db.data_test.find({"rank":{$size:3}})//查询rank数组大小为3的记录
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
- "game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
- "game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
- "game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
- "game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
- "game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
- "game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
- "game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
- "game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
- "game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
- { "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
- { "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
2
.MongoDB JAVA API高级查询示例
- /**
- * MongoDBTest
- * MongoDB JAVA API的高级查询示例
- */
- package com.labci.mongodb.test;
- import java.net.UnknownHostException;
- import java.util.Iterator;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.Mongo;
- import com.mongodb.MongoException;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 21, 2011[11:33:34 PM]
- *
- */
- public class MongoDBAdvancedQuery {
- private static final String HOST = "192.168.1.86";
- private static final int PORT = 27017;
- private static final String USER = "iwtxokhtd";
- private static final String PASSWORD = "123456";
- private static final String DB_NAME = "test";
- private static final String COLLECTION = "data_test";
- private static Mongo conn=null;
- private static DB myDB=null;
- private static DBCollection myCollection=null;
- static{
- try {
- conn=new Mongo(HOST,PORT);//建立数据库连接
- myDB=conn.getDB(DB_NAME);//使用test数据库
- boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证
- if(loginSuccess){
- myCollection=myDB.getCollection(COLLECTION);
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (MongoException e) {
- e.printStackTrace();
- }
- }
- /**
- * 查询数据
- * @param collection “表”名
- */
- private static void queryData(DBCollection collection){
- //count查询
- System.out.println("表的总记录数为:");
- System.out.println(collection.find().count());
- //分页查询
- DBCursor findAll=collection.find();
- DBCursor queryByPage=findAll.skip(3).limit(4);
- printData("从第3条记录起取4条记录为:",queryByPage);
- //order by操作
- DBObject orderBy = new BasicDBObject();
- orderBy.put("userName", -1);//按userName倒序排
- DBCursor orderByResult=collection.find().sort(orderBy);
- printData("所有记录按userName倒序排为:",orderByResult);
- // "!=" 和 ">" 操作
- DBObject notEqual=new BasicDBObject();
- notEqual.put("$ne", "Bill Tu10");
- DBObject greatThan=new BasicDBObject();
- greatThan.put("$gt", 7);
- DBObject notEqualAndGreatThan=new BasicDBObject();
- notEqualAndGreatThan.put("userName", notEqual);
- notEqualAndGreatThan.put("age", greatThan);
- DBCursor notEqualAndGreatThanResult=collection.find(notEqualAndGreatThan);
- printData("userName!='Bill Tu10' and age>7的记录为:",notEqualAndGreatThanResult);
- // ">=" 和"<="操作
- DBObject greatThanEqualAndLessThanEqual=new BasicDBObject();
- greatThanEqualAndLessThanEqual.put("$gte", 2);
- greatThanEqualAndLessThanEqual.put("$lte", 7);
- DBObject ageCompare=new BasicDBObject();
- ageCompare.put("age",greatThanEqualAndLessThanEqual);
- DBCursor compareResult=collection.find(ageCompare);
- printData("age>=2 and age<=7的记录为:",compareResult);
- // all操作
- DBObject all=new BasicDBObject();
- all.put("$all", new Object[]{7,7});
- DBObject rankAll=new BasicDBObject();
- rankAll.put("rank", all);
- DBCursor rankAllResult=collection.find(rankAll);
- printData("rank in all(7,7)的记录为:",rankAllResult);
- //not in操作
- DBObject notIn=new BasicDBObject();
- notIn.put("$nin", new Object[]{2,3});
- DBObject ageNotIn=new BasicDBObject();
- ageNotIn.put("age", notIn);
- DBCursor ageNotInResult=collection.find(ageNotIn);
- printData("age not in (2,3)的记录为:",ageNotInResult);
- //or操作
- DBObject orGreatThan=new BasicDBObject("$gt",3);
- DBObject orRankAll=new BasicDBObject("$all",new Object[]{1,1});
- DBObject ageOrGreatThan=new BasicDBObject();
- ageOrGreatThan.put("age", orGreatThan);
- DBObject rankOrAll=new BasicDBObject();
- rankOrAll.put("rank", orRankAll);
- DBObject orOperation=new BasicDBObject();
- orOperation.put("$or", new Object[]{ageOrGreatThan,rankOrAll});
- DBCursor orResult=collection.find(orOperation);
- printData("age>3 or rank in all(1,1)的记录为:",orResult);
- //not or操作
- DBObject notOrOperation=new BasicDBObject();
- notOrOperation.put("$nor", new Object[]{ageOrGreatThan,rankOrAll});
- DBCursor notOrResult=collection.find(notOrOperation);
- printData("not(age>3 or rank in all(1,1))的记录为:",notOrResult);
- //size 操作
- DBObject size=new BasicDBObject("$size",3);
- DBObject rankSize=new BasicDBObject();
- rankSize.put("rank", size);
- DBCursor sizeResult=collection.find(rankSize);
- printData("rank数组大小为3的记录为:",sizeResult);
- //exists操作
- DBObject exists=new BasicDBObject("$exists",false);
- DBObject userNameExists=new BasicDBObject();
- userNameExists.put("userName", exists);
- DBCursor userNameExistsResult=collection.find(userNameExists);
- printData("userName exists false的记录为:",userNameExistsResult);
- //mod 取模操作
- DBObject modArray=new BasicDBObject("$mod",new Object[]{2,0});
- DBObject ageMod=new BasicDBObject();
- ageMod.put("age", modArray);
- DBCursor ageModResult=collection.find(ageMod);
- printData("age%2==0的记录为:",ageModResult);
- }
- /**
- * 打印结果数据
- * @param description 结果数据相关描述
- * @param recordResult 结果集
- */
- private static void printData(String description,DBCursor recordResult){
- System.out.println(description);
- for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){
- System.out.println(iter.next());
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- queryData(myCollection);
- }
- }
对应的查询结果:
- 表的总记录数为:
- 10
- 从第3条记录起取4条记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- 所有记录按userName倒序排为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}
- userName!='Bill Tu10' and age>7的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}
- age>=2 and age<=7的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- rank in all(7,7)的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- age not in (2,3)的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}
- age>3 or rank in all(1,1)的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}
- not(age>3 or rank in all(1,1))的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}
- rank数组大小为3的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f3"} , "userId" : "10010171" , "userName" : "Bill Tu1" , "gender" : "m1" , "age" : 1 , "rank" : [ 1 , 1 , 1] , "interests" : { "game" : "game1" , "ball" : "ball1" , "other" : "nothing1"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f5"} , "userId" : "10010173" , "userName" : "Bill Tu3" , "gender" : "m3" , "age" : 3 , "rank" : [ 3 , 3 , 3] , "interests" : { "game" : "game3" , "ball" : "ball3" , "other" : "nothing3"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f7"} , "userId" : "10010175" , "userName" : "Bill Tu5" , "gender" : "m5" , "age" : 5 , "rank" : [ 5 , 5 , 5] , "interests" : { "game" : "game5" , "ball" : "ball5" , "other" : "nothing5"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f9"} , "userId" : "10010177" , "userName" : "Bill Tu7" , "gender" : "m7" , "age" : 7 , "rank" : [ 7 , 7 , 7] , "interests" : { "game" : "game7" , "ball" : "ball7" , "other" : "nothing7"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fb"} , "userId" : "10010179" , "userName" : "Bill Tu9" , "gender" : "m9" , "age" : 9 , "rank" : [ 9 , 9 , 9] , "interests" : { "game" : "game9" , "ball" : "ball9" , "other" : "nothing9"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}
- userName exists false的记录为:
- age%2==0的记录为:
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f4"} , "userId" : "10010172" , "userName" : "Bill Tu2" , "gender" : "m2" , "age" : 2 , "rank" : [ 2 , 2 , 2] , "interests" : { "game" : "game2" , "ball" : "ball2" , "other" : "nothing2"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f6"} , "userId" : "10010174" , "userName" : "Bill Tu4" , "gender" : "m4" , "age" : 4 , "rank" : [ 4 , 4 , 4] , "interests" : { "game" : "game4" , "ball" : "ball4" , "other" : "nothing4"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37f8"} , "userId" : "10010176" , "userName" : "Bill Tu6" , "gender" : "m6" , "age" : 6 , "rank" : [ 6 , 6 , 6] , "interests" : { "game" : "game6" , "ball" : "ball6" , "other" : "nothing6"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fa"} , "userId" : "10010178" , "userName" : "Bill Tu8" , "gender" : "m8" , "age" : 8 , "rank" : [ 8 , 8 , 8] , "interests" : { "game" : "game8" , "ball" : "ball8" , "other" : "nothing8"}}
- { "_id" : { "$oid" : "4dd8a274b2d556790b0b37fc"} , "userId" : "100101710" , "userName" : "Bill Tu10" , "gender" : "m10" , "age" : 10 , "rank" : [ 10 , 10 , 10] , "interests" : { "game" : "game10" , "ball" : "ball10" , "other" : "nothing10"}}
- /**
- * MongoDBTest
- * MongoDB插入和删除数据示例
- */
- package com.labci.mongodb.test;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.Mongo;
- import com.mongodb.MongoException;
- /**
- *
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 22, 2011[3:10:56 PM]
- *
- */
- public class MongoDBInsertDataTest {
- private static final String HOST = "192.168.1.86";
- private static final int PORT = 27017;
- private static final String USER = "iwtxokhtd";
- private static final String PASSWORD = "123456";
- private static final String DB_NAME = "test";
- private static final String COLLECTION = "insert_test";
- private static Mongo conn=null;
- private static DB myDB=null;
- private static DBCollection myCollection=null;
- static{
- try {
- conn=new Mongo(HOST,PORT);//建立数据库连接
- myDB=conn.getDB(DB_NAME);//使用test数据库
- boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证
- if(loginSuccess){
- myCollection=myDB.getCollection(COLLECTION);
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (MongoException e) {
- e.printStackTrace();
- }
- }
- /**
- * 使用save()方法保存数据
- * @param collection “表”名
- */
- private static void saveData(DBCollection collection){
- DBObject saveData=new BasicDBObject();
- saveData.put("userName", "iwtxokhtd");
- saveData.put("age", "26");
- saveData.put("gender", "m");
- DBObject infoData=new BasicDBObject();
- infoData.put("height", 16.3);
- infoData.put("weight", 22);
- saveData.put("info", infoData);
- collection.save(saveData);
- }
- /**
- * 使用insert()方法保存数据
- * @param collection "表"名
- */
- private static void insertData(DBCollection collection){
- DBObject insertData=new BasicDBObject();
- insertData.put("name", "pig");
- insertData.put("headers", 2);
- insertData.put("legs", 4);
- //insert(DBObject obj)
- collection.insert(insertData);
- DBObject insertDataFox=new BasicDBObject();
- insertDataFox.put("name", "fox");
- insertDataFox.put("headers", 1);
- insertDataFox.put("legs", 4);
- DBObject insertDataTiger=new BasicDBObject();
- insertDataTiger.put("name", "tiger");
- insertDataTiger.put("headers", 1);
- insertDataTiger.put("legs", 3);
- List<DBObject> insertDataList=new ArrayList<DBObject>();
- insertDataList.add(insertDataFox);
- insertDataList.add(insertDataTiger);
- //insert(List<DBObject> list)
- collection.insert(insertDataList);
- }
- /**
- * 取得查询结果集
- * @param collection
- * @return
- */
- private static DBCursor getResult(DBCollection collection){
- return collection.find();
- }
- /**
- * 打印结果数据
- * @param description 结果数据相关描述
- * @param recordResult 结果集
- */
- private static void printData(String description,DBCursor recordResult){
- System.out.println(description);
- for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){
- System.out.println(iter.next());
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- //插入数据前若有记录才先删除所有记录
- if(myCollection.find().count()>0){
- myCollection.remove(new BasicDBObject());
- }
- //再插入数据
- saveData(myCollection);
- insertData(myCollection);
- //再查询数据
- printData("插入数据后的所有结果记录为:",getResult(myCollection));
- //再删除指定的数据
- DBObject deletePig=new BasicDBObject();
- deletePig.put("name", "pig");
- myCollection.remove(deletePig);
- printData("删除pig后的所有结果记录为:",getResult(myCollection));
- }
- }
结果:
- 插入数据后的所有结果记录为:
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af38"} , "userName" : "iwtxokhtd" , "age" : "26" , "gender" : "m" , "info" : { "height" : 16.3 , "weight" : 22}}
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af39"} , "name" : "pig" , "headers" : 2 , "legs" : 4}
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3a"} , "name" : "fox" , "headers" : 1 , "legs" : 4}
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3b"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}
- 删除pig后的所有结果记录为:
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af38"} , "userName" : "iwtxokhtd" , "age" : "26" , "gender" : "m" , "info" : { "height" : 16.3 , "weight" : 22}}
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3a"} , "name" : "fox" , "headers" : 1 , "legs" : 4}
- { "_id" : { "$oid" : "4dd8c190b2d57bb3ba03af3b"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}
- /**
- * MongoDBTest
- * MongoDB更新数据使用示例
- */
- package com.labci.mongodb.test;
- import java.net.UnknownHostException;
- import java.util.Iterator;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.Mongo;
- import com.mongodb.MongoException;
- /**
- *
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 22, 2011[3:10:56 PM]
- *
- */
- public class MongoDBUpdateDataTest {
- private static final String HOST = "192.168.1.86";
- private static final int PORT = 27017;
- private static final String USER = "iwtxokhtd";
- private static final String PASSWORD = "123456";
- private static final String DB_NAME = "test";
- private static final String COLLECTION = "insert_test";
- private static Mongo conn=null;
- private static DB myDB=null;
- private static DBCollection myCollection=null;
- static{
- try {
- conn=new Mongo(HOST,PORT);//建立数据库连接
- myDB=conn.getDB(DB_NAME);//使用test数据库
- boolean loginSuccess=myDB.authenticate(USER, PASSWORD.toCharArray());//用户验证
- if(loginSuccess){
- myCollection=myDB.getCollection(COLLECTION);
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (MongoException e) {
- e.printStackTrace();
- }
- }
- /**
- * 更新“表”数据
- * @param collection
- */
- private static void updateData(DBCollection collection){
- DBObject updateCondition=new BasicDBObject();
- //where name='fox'
- updateCondition.put("name", "fox");
- DBObject updatedValue=new BasicDBObject();
- updatedValue.put("headers", 3);
- updatedValue.put("legs", 4);
- DBObject updateSetValue=new BasicDBObject("$set",updatedValue);
- /**
- * update insert_test set headers=3 and legs=4 where name='fox'
- * updateCondition:更新条件
- * updateSetValue:设置的新值
- */
- collection.update(updateCondition, updateSetValue);
- DBObject queryCondition=new BasicDBObject();
- //where name='sam',此条件在更新前不是成立的
- queryCondition.put("name", "sam");
- DBObject setValue=new BasicDBObject();
- setValue.put("headers", 1);
- setValue.put("legs", 1);
- DBObject upsertValue=new BasicDBObject("$set",setValue);
- /**
- * 后面两个参数含义分别是:
- * 若所更新的数据没有,则插入
- * ,同时更新多个符合条件的文档(collection)
- */
- collection.update(queryCondition, upsertValue, true, true);
- //set headers=headers+2
- DBObject incValue=new BasicDBObject("headers",2);
- //set legs=4
- DBObject legsValue=new BasicDBObject("legs",4);
- DBObject allCondition=new BasicDBObject();
- allCondition.put("$inc", incValue);
- allCondition.put("$set", legsValue);
- collection.update(queryCondition, allCondition);
- }
- /**
- * 返回查询结果集
- * @param collection
- * @return
- */
- private static DBCursor queryData(DBCollection collection){
- DBCursor queriedData=collection.find();
- return queriedData;
- }
- /**
- * 打印结果数据
- * @param description 结果数据相关描述
- * @param recordResult 结果集
- */
- private static void printData(String description,DBCursor recordResult){
- System.out.println(description);
- for(Iterator<DBObject> iter=recordResult.iterator();iter.hasNext();){
- System.out.println(iter.next());
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- printData("查看一下更新前的数据:",queryData(myCollection));
- //更新数据
- updateData(myCollection);
- printData("查看一下更新后的数据:",queryData(myCollection));
- }
- }
运行结果:
- 查看一下更新前的数据:
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}
- { "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 1 , "legs" : 1 , "name" : "sam"}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height" : 16.3 , "weight" : 22} , "legs" : 4 , "userName" : "iwtxokhtd"}
- 查看一下更新后的数据:
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce4"} , "name" : "fox" , "headers" : 3 , "legs" : 4}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce5"} , "name" : "tiger" , "headers" : 1 , "legs" : 3}
- { "_id" : { "$oid" : "4dda4840cfce8015e1824188"} , "headers" : 3 , "legs" : 4 , "name" : "sam"}
- { "_id" : { "$oid" : "4dda4342b2d5a428449b7ce2"} , "age" : "26" , "gender" : "m" , "headers" : 2 , "info" : { "height"
本文转载自: 点击打开链接
作者 :
tujiyue