mongodb使用
mongodb使用
1. 下载 mongo-2.6.3.jar
2. 新建 java项目
3. 按Spring方式封装查询 直接上代码
Java代码
1.package com.mytest;
2.
3.import java.net.UnknownHostException;
4.
5.import com.mongodb.DB;
6.import com.mongodb.DBCollection;
7.import com.mongodb.Mongo;
8.import com.mongodb.MongoException;
9.
10./**
11. *
12. *
13. * @author lw
14. * @created 2011-6-27 下午04:26:40
15. * @version 1.0.0
16. * @date 2011-6-27 下午04:26:40
17. */
18.
19.public class DBTemplate {
20. private static String MONGODB_SERVER = "192.168.42.212";
21. private static int SERVER_PORT = 27017;
22. private static String MONGODB_DBNAME = "test";
23.
24. public final Object execute(MsgCallback action, String collection) {
25. DB db = getConn();
26. DBCollection dbColl = db.getCollection(collection);
27. Object result = action.doExecute(dbColl);
28. closeDb(db);
29. closeCollection(dbColl);
30.
31. return result;
32. }
33.
34. private DB getConn() {
35. return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME);
36. }
37.
38. private DB getConn(String server, int port, String dbName) {
39. Mongo m = null;
40. try {
41. m = new Mongo(server, port);
42. } catch (UnknownHostException e) {
43. e.printStackTrace();
44. } catch (MongoException e) {
45. e.printStackTrace();
46. }
47. return m.getDB(dbName);
48. }
49.
50. private void closeDb(DB db) {
51. if (db != null) {
52. db = null;
53. }
54. }
55.
56. private void closeCollection(DBCollection col) {
57. if (col != null) {
58. col = null;
59. }
60. }
61.}
package com.mytest;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
*
*
* @author lw
* @created 2011-6-27 下午04:26:40
* @version 1.0.0
* @date 2011-6-27 下午04:26:40
*/
public class DBTemplate {
private static String MONGODB_SERVER = "192.168.42.212";
private static int SERVER_PORT = 27017;
private static String MONGODB_DBNAME = "test";
public final Object execute(MsgCallback action, String collection) {
DB db = getConn();
DBCollection dbColl = db.getCollection(collection);
Object result = action.doExecute(dbColl);
closeDb(db);
closeCollection(dbColl);
return result;
}
private DB getConn() {
return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME);
}
private DB getConn(String server, int port, String dbName) {
Mongo m = null;
try {
m = new Mongo(server, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
return m.getDB(dbName);
}
private void closeDb(DB db) {
if (db != null) {
db = null;
}
}
private void closeCollection(DBCollection col) {
if (col != null) {
col = null;
}
}
}
Java代码
1.package com.mytest;
2.
3.import com.mongodb.DBCollection;
4.
5./**
6. *
7. * 功能描述:
8. *
9. * @author lw
10. * @created 2011-6-28 下午01:57:44
11. * @version 1.0.0
12. * @date 2011-6-28 下午01:57:44
13. */
14.
15.public interface MsgCallback {
16. Object doExecute(DBCollection dbCollection);
17.}
package com.mytest;
import com.mongodb.DBCollection;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午01:57:44
* @version 1.0.0
* @date 2011-6-28 下午01:57:44
*/
public interface MsgCallback {
Object doExecute(DBCollection dbCollection);
}
Java代码
1.package com.mytest;
2.
3.import java.util.List;
4.import java.util.Map;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBObject;
8.
9./**
10. *
11. * 功能描述:
12. *
13. * @author lw
14. * @created 2011-6-28 下午02:13:33
15. * @version 1.0.0
16. * @date 2011-6-28 下午02:13:33
17. */
18.
19.public interface SQLTemplate {
20.
21. int insert(String collection, BasicDBObject dbObj);
22.
23. int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
24.
25. int delete(String collection, BasicDBObject dbObj);
26.
27. Object selectOne(String collection, BasicDBObject dbObj);
28.
29. Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
30.
31. List<DBObject> selectList(String collection, final BasicDBObject dbObj);
32.
33.}
package com.mytest;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:13:33
* @version 1.0.0
* @date 2011-6-28 下午02:13:33
*/
public interface SQLTemplate {
int insert(String collection, BasicDBObject dbObj);
int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
int delete(String collection, BasicDBObject dbObj);
Object selectOne(String collection, BasicDBObject dbObj);
Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
List<DBObject> selectList(String collection, final BasicDBObject dbObj);
}
Java代码
1.package com.mytest;
2.
3.import java.util.List;
4.import java.util.Map;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBObject;
8.
9./**
10. *
11. * 功能描述:
12. *
13. * @author lw
14. * @created 2011-6-28 下午02:13:33
15. * @version 1.0.0
16. * @date 2011-6-28 下午02:13:33
17. */
18.
19.public interface SQLTemplate {
20.
21. int insert(String collection, BasicDBObject dbObj);
22.
23. int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
24.
25. int delete(String collection, BasicDBObject dbObj);
26.
27. Object selectOne(String collection, BasicDBObject dbObj);
28.
29. Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
30.
31. List<DBObject> selectList(String collection, final BasicDBObject dbObj);
32.
33.}
package com.mytest;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:13:33
* @version 1.0.0
* @date 2011-6-28 下午02:13:33
*/
public interface SQLTemplate {
int insert(String collection, BasicDBObject dbObj);
int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
int delete(String collection, BasicDBObject dbObj);
Object selectOne(String collection, BasicDBObject dbObj);
Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
List<DBObject> selectList(String collection, final BasicDBObject dbObj);
}
Java代码
1.package com.mytest;
2.
3.import java.util.List;
4.import java.util.Map;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBCollection;
8.import com.mongodb.DBObject;
9.
10./**
11. *
12. * 功能描述:
13. *
14. * @author lw
15. * @created 2011-6-28 下午02:21:43
16. * @version 1.0.0
17. * @date 2011-6-28 下午02:21:43
18. */
19.
20.public class SQLDao implements SQLTemplate {
21. //可改成 Spring 注入
22. DBTemplate dbTemp = new DBTemplate();
23.
24. public int insert(String collection, final BasicDBObject dbObj) {
25. return (Integer) dbTemp.execute(new MsgCallback() {
26. public Object doExecute(DBCollection dbCollection) {
27. return dbCollection.insert(dbObj).getN();
28. }
29. }, collection);
30. }
31.
32. public int update(String collection, final BasicDBObject oldObj,
33. final BasicDBObject newObj) {
34. return (Integer) dbTemp.execute(new MsgCallback() {
35. public Object doExecute(DBCollection dbCollection) {
36. return dbCollection.update(oldObj, newObj).getN();
37. }
38. }, collection);
39. }
40.
41. public int delete(String collection, final BasicDBObject dbObj) {
42. return (Integer) dbTemp.execute(new MsgCallback() {
43. public Object doExecute(DBCollection dbCollection) {
44. return dbCollection.remove(dbObj).getN();
45. }
46. }, collection);
47. }
48.
49. public Object selectOne(String collection, final BasicDBObject dbObj) {
50. return dbTemp.execute(new MsgCallback() {
51. public Object doExecute(DBCollection dbCollection) {
52. return dbCollection.findOne(dbObj);
53. }
54. }, collection);
55. }
56.
57. @SuppressWarnings("unchecked")
58. public Map<String, DBObject> selectMap(String collection,
59. final BasicDBObject dbObj) {
60. return ((DBObject) selectOne(collection, dbObj)).toMap();
61. }
62.
63. @SuppressWarnings("unchecked")
64. public List<DBObject> selectList(String collection,
65. final BasicDBObject dbObj) {
66. return (List<DBObject>) dbTemp.execute(new MsgCallback() {
67. public Object doExecute(DBCollection dbCollection) {
68. return dbCollection.find(dbObj).toArray();
69. }
70. }, collection);
71. }
72.}
package com.mytest;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:21:43
* @version 1.0.0
* @date 2011-6-28 下午02:21:43
*/
public class SQLDao implements SQLTemplate {
//可改成 Spring 注入
DBTemplate dbTemp = new DBTemplate();
public int insert(String collection, final BasicDBObject dbObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.insert(dbObj).getN();
}
}, collection);
}
public int update(String collection, final BasicDBObject oldObj,
final BasicDBObject newObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.update(oldObj, newObj).getN();
}
}, collection);
}
public int delete(String collection, final BasicDBObject dbObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.remove(dbObj).getN();
}
}, collection);
}
public Object selectOne(String collection, final BasicDBObject dbObj) {
return dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.findOne(dbObj);
}
}, collection);
}
@SuppressWarnings("unchecked")
public Map<String, DBObject> selectMap(String collection,
final BasicDBObject dbObj) {
return ((DBObject) selectOne(collection, dbObj)).toMap();
}
@SuppressWarnings("unchecked")
public List<DBObject> selectList(String collection,
final BasicDBObject dbObj) {
return (List<DBObject>) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.find(dbObj).toArray();
}
}, collection);
}
}
Java代码
1.package com.mytest;
2.
3.import java.util.ArrayList;
4.import java.util.List;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBObject;
8.import com.mongodb.util.JSON;
9.
10./**
11. *
12. * 功能描述:
13. *
14. * @author lw
15. * @created 2011-6-28 下午03:31:51
16. * @version 1.0.0
17. * @date 2011-6-28 下午03:31:51
18. */
19.
20.public class TestSQLDao extends SQLDao {
21.
22. /**
23. * 功能描述:
24. *
25. * @param args
26. */
27.
28. public static void main(String[] args) {
29. TestSQLDao test = new TestSQLDao();
30.
31. BasicDBObject obj = new BasicDBObject();
32. // obj.put("id", 6);
33. obj.put("name", "pluto");
34. // BasicDBObject newObj = new BasicDBObject("$set", new
35. // BasicDBObject("name", "gogo"));
36. List<DBObject> list = new ArrayList<DBObject>();
37. list = test.selectList("students", obj);
38.
39. for (DBObject db : list) {
40. System.out
41. .println("-----------------------------------------------------");
42. System.out.println(JSON.serialize(db));
43. System.out
44. .println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
45. System.out.println(db.toMap());
46. System.out
47. .println("-----------------------------------------------------");
48. }
49. }
50.
51. public int insert(String collection, BasicDBObject dbObj) {
52. return super.insert(collection, dbObj);
53. }
54.
55. public List<DBObject> selectList(String collection, BasicDBObject dbObj) {
56. return super.selectList(collection, dbObj);
57. }
58.
59.}
1. 下载 mongo-2.6.3.jar
2. 新建 java项目
3. 按Spring方式封装查询 直接上代码
Java代码
1.package com.mytest;
2.
3.import java.net.UnknownHostException;
4.
5.import com.mongodb.DB;
6.import com.mongodb.DBCollection;
7.import com.mongodb.Mongo;
8.import com.mongodb.MongoException;
9.
10./**
11. *
12. *
13. * @author lw
14. * @created 2011-6-27 下午04:26:40
15. * @version 1.0.0
16. * @date 2011-6-27 下午04:26:40
17. */
18.
19.public class DBTemplate {
20. private static String MONGODB_SERVER = "192.168.42.212";
21. private static int SERVER_PORT = 27017;
22. private static String MONGODB_DBNAME = "test";
23.
24. public final Object execute(MsgCallback action, String collection) {
25. DB db = getConn();
26. DBCollection dbColl = db.getCollection(collection);
27. Object result = action.doExecute(dbColl);
28. closeDb(db);
29. closeCollection(dbColl);
30.
31. return result;
32. }
33.
34. private DB getConn() {
35. return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME);
36. }
37.
38. private DB getConn(String server, int port, String dbName) {
39. Mongo m = null;
40. try {
41. m = new Mongo(server, port);
42. } catch (UnknownHostException e) {
43. e.printStackTrace();
44. } catch (MongoException e) {
45. e.printStackTrace();
46. }
47. return m.getDB(dbName);
48. }
49.
50. private void closeDb(DB db) {
51. if (db != null) {
52. db = null;
53. }
54. }
55.
56. private void closeCollection(DBCollection col) {
57. if (col != null) {
58. col = null;
59. }
60. }
61.}
package com.mytest;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
*
*
* @author lw
* @created 2011-6-27 下午04:26:40
* @version 1.0.0
* @date 2011-6-27 下午04:26:40
*/
public class DBTemplate {
private static String MONGODB_SERVER = "192.168.42.212";
private static int SERVER_PORT = 27017;
private static String MONGODB_DBNAME = "test";
public final Object execute(MsgCallback action, String collection) {
DB db = getConn();
DBCollection dbColl = db.getCollection(collection);
Object result = action.doExecute(dbColl);
closeDb(db);
closeCollection(dbColl);
return result;
}
private DB getConn() {
return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME);
}
private DB getConn(String server, int port, String dbName) {
Mongo m = null;
try {
m = new Mongo(server, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
return m.getDB(dbName);
}
private void closeDb(DB db) {
if (db != null) {
db = null;
}
}
private void closeCollection(DBCollection col) {
if (col != null) {
col = null;
}
}
}
Java代码
1.package com.mytest;
2.
3.import com.mongodb.DBCollection;
4.
5./**
6. *
7. * 功能描述:
8. *
9. * @author lw
10. * @created 2011-6-28 下午01:57:44
11. * @version 1.0.0
12. * @date 2011-6-28 下午01:57:44
13. */
14.
15.public interface MsgCallback {
16. Object doExecute(DBCollection dbCollection);
17.}
package com.mytest;
import com.mongodb.DBCollection;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午01:57:44
* @version 1.0.0
* @date 2011-6-28 下午01:57:44
*/
public interface MsgCallback {
Object doExecute(DBCollection dbCollection);
}
Java代码
1.package com.mytest;
2.
3.import java.util.List;
4.import java.util.Map;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBObject;
8.
9./**
10. *
11. * 功能描述:
12. *
13. * @author lw
14. * @created 2011-6-28 下午02:13:33
15. * @version 1.0.0
16. * @date 2011-6-28 下午02:13:33
17. */
18.
19.public interface SQLTemplate {
20.
21. int insert(String collection, BasicDBObject dbObj);
22.
23. int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
24.
25. int delete(String collection, BasicDBObject dbObj);
26.
27. Object selectOne(String collection, BasicDBObject dbObj);
28.
29. Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
30.
31. List<DBObject> selectList(String collection, final BasicDBObject dbObj);
32.
33.}
package com.mytest;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:13:33
* @version 1.0.0
* @date 2011-6-28 下午02:13:33
*/
public interface SQLTemplate {
int insert(String collection, BasicDBObject dbObj);
int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
int delete(String collection, BasicDBObject dbObj);
Object selectOne(String collection, BasicDBObject dbObj);
Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
List<DBObject> selectList(String collection, final BasicDBObject dbObj);
}
Java代码
1.package com.mytest;
2.
3.import java.util.List;
4.import java.util.Map;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBObject;
8.
9./**
10. *
11. * 功能描述:
12. *
13. * @author lw
14. * @created 2011-6-28 下午02:13:33
15. * @version 1.0.0
16. * @date 2011-6-28 下午02:13:33
17. */
18.
19.public interface SQLTemplate {
20.
21. int insert(String collection, BasicDBObject dbObj);
22.
23. int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
24.
25. int delete(String collection, BasicDBObject dbObj);
26.
27. Object selectOne(String collection, BasicDBObject dbObj);
28.
29. Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
30.
31. List<DBObject> selectList(String collection, final BasicDBObject dbObj);
32.
33.}
package com.mytest;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:13:33
* @version 1.0.0
* @date 2011-6-28 下午02:13:33
*/
public interface SQLTemplate {
int insert(String collection, BasicDBObject dbObj);
int update(String collection, BasicDBObject oldObj, BasicDBObject newObj);
int delete(String collection, BasicDBObject dbObj);
Object selectOne(String collection, BasicDBObject dbObj);
Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj);
List<DBObject> selectList(String collection, final BasicDBObject dbObj);
}
Java代码
1.package com.mytest;
2.
3.import java.util.List;
4.import java.util.Map;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBCollection;
8.import com.mongodb.DBObject;
9.
10./**
11. *
12. * 功能描述:
13. *
14. * @author lw
15. * @created 2011-6-28 下午02:21:43
16. * @version 1.0.0
17. * @date 2011-6-28 下午02:21:43
18. */
19.
20.public class SQLDao implements SQLTemplate {
21. //可改成 Spring 注入
22. DBTemplate dbTemp = new DBTemplate();
23.
24. public int insert(String collection, final BasicDBObject dbObj) {
25. return (Integer) dbTemp.execute(new MsgCallback() {
26. public Object doExecute(DBCollection dbCollection) {
27. return dbCollection.insert(dbObj).getN();
28. }
29. }, collection);
30. }
31.
32. public int update(String collection, final BasicDBObject oldObj,
33. final BasicDBObject newObj) {
34. return (Integer) dbTemp.execute(new MsgCallback() {
35. public Object doExecute(DBCollection dbCollection) {
36. return dbCollection.update(oldObj, newObj).getN();
37. }
38. }, collection);
39. }
40.
41. public int delete(String collection, final BasicDBObject dbObj) {
42. return (Integer) dbTemp.execute(new MsgCallback() {
43. public Object doExecute(DBCollection dbCollection) {
44. return dbCollection.remove(dbObj).getN();
45. }
46. }, collection);
47. }
48.
49. public Object selectOne(String collection, final BasicDBObject dbObj) {
50. return dbTemp.execute(new MsgCallback() {
51. public Object doExecute(DBCollection dbCollection) {
52. return dbCollection.findOne(dbObj);
53. }
54. }, collection);
55. }
56.
57. @SuppressWarnings("unchecked")
58. public Map<String, DBObject> selectMap(String collection,
59. final BasicDBObject dbObj) {
60. return ((DBObject) selectOne(collection, dbObj)).toMap();
61. }
62.
63. @SuppressWarnings("unchecked")
64. public List<DBObject> selectList(String collection,
65. final BasicDBObject dbObj) {
66. return (List<DBObject>) dbTemp.execute(new MsgCallback() {
67. public Object doExecute(DBCollection dbCollection) {
68. return dbCollection.find(dbObj).toArray();
69. }
70. }, collection);
71. }
72.}
package com.mytest;
import java.util.List;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
/**
*
* 功能描述:
*
* @author lw
* @created 2011-6-28 下午02:21:43
* @version 1.0.0
* @date 2011-6-28 下午02:21:43
*/
public class SQLDao implements SQLTemplate {
//可改成 Spring 注入
DBTemplate dbTemp = new DBTemplate();
public int insert(String collection, final BasicDBObject dbObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.insert(dbObj).getN();
}
}, collection);
}
public int update(String collection, final BasicDBObject oldObj,
final BasicDBObject newObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.update(oldObj, newObj).getN();
}
}, collection);
}
public int delete(String collection, final BasicDBObject dbObj) {
return (Integer) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.remove(dbObj).getN();
}
}, collection);
}
public Object selectOne(String collection, final BasicDBObject dbObj) {
return dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.findOne(dbObj);
}
}, collection);
}
@SuppressWarnings("unchecked")
public Map<String, DBObject> selectMap(String collection,
final BasicDBObject dbObj) {
return ((DBObject) selectOne(collection, dbObj)).toMap();
}
@SuppressWarnings("unchecked")
public List<DBObject> selectList(String collection,
final BasicDBObject dbObj) {
return (List<DBObject>) dbTemp.execute(new MsgCallback() {
public Object doExecute(DBCollection dbCollection) {
return dbCollection.find(dbObj).toArray();
}
}, collection);
}
}
Java代码
1.package com.mytest;
2.
3.import java.util.ArrayList;
4.import java.util.List;
5.
6.import com.mongodb.BasicDBObject;
7.import com.mongodb.DBObject;
8.import com.mongodb.util.JSON;
9.
10./**
11. *
12. * 功能描述:
13. *
14. * @author lw
15. * @created 2011-6-28 下午03:31:51
16. * @version 1.0.0
17. * @date 2011-6-28 下午03:31:51
18. */
19.
20.public class TestSQLDao extends SQLDao {
21.
22. /**
23. * 功能描述:
24. *
25. * @param args
26. */
27.
28. public static void main(String[] args) {
29. TestSQLDao test = new TestSQLDao();
30.
31. BasicDBObject obj = new BasicDBObject();
32. // obj.put("id", 6);
33. obj.put("name", "pluto");
34. // BasicDBObject newObj = new BasicDBObject("$set", new
35. // BasicDBObject("name", "gogo"));
36. List<DBObject> list = new ArrayList<DBObject>();
37. list = test.selectList("students", obj);
38.
39. for (DBObject db : list) {
40. System.out
41. .println("-----------------------------------------------------");
42. System.out.println(JSON.serialize(db));
43. System.out
44. .println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
45. System.out.println(db.toMap());
46. System.out
47. .println("-----------------------------------------------------");
48. }
49. }
50.
51. public int insert(String collection, BasicDBObject dbObj) {
52. return super.insert(collection, dbObj);
53. }
54.
55. public List<DBObject> selectList(String collection, BasicDBObject dbObj) {
56. return super.selectList(collection, dbObj);
57. }
58.
59.}
相关推荐
MongoDB使用手册是数据库管理员和开发者的宝贵资源,它涵盖了MongoDB的各种操作、管理以及最佳实践。MongoDB是一个流行的开源、非关系型数据库系统,以其灵活性、可扩展性和高性能著称。以下是一些核心的MongoDB知识...
Java 中 MongoDB 使用环境详细配置 Java 中 MongoDB 使用环境详细配置是指在 Windows 平台(WinXP)上安装、运行、安装 Windows 服务的笔记,以作备忘。下面是一些重要的知识点: 1. MongoDB 的下载和安装 ...
mongoDB使用.gomongoDB使用.gomongoDB使用.go
"MongoDB使用教程" MongoDB是一个基于分布式文件存储的NoSQL数据库,具有高性能、灵活的数据模型和可扩展的架构。下面是MongoDB使用教程的详细知识点: 一、MongoDB Atlas 云数据库(免费) MongoDB Atlas是...
MongoDB使用手册是一份详尽的指南,专为想要深入理解和掌握MongoDB配置与使用的人士准备。MongoDB是一款流行的开源、文档型数据库系统,以其灵活性、可扩展性和高性能著称。这份手册将涵盖以下几个核心知识点: 1. ...
MongoDB 使用手册 MongoDB 是一个高性能、开源、无模式的文档型数据库,适用于现代应用程序开发。它以其灵活的数据模型、强大的查询能力以及分布式架构而备受青睐。本手册将涵盖MongoDB的安装、使用和基本语法,...
MongoDB使用大全的知识点 NoSQL简介: NoSQL是一种非关系型数据库管理系统,其设计是为了应对大规模数据集的存储和管理。由于NoSQL数据库的灵活性和扩展性,它们在处理高并发和大数据方面具有显著优势。 MongoDB...
mongodb数据库 Python 相互协作 使用 方法 介绍
本文档是Spring-data-mongodb使用手册;感兴趣的朋友可以过来看看
使用MyBatisPlus的方式,优雅的操作MongoDB使用MyBatisPlus的方式,优雅的操作MongoDB使用MyBatisPlus的方式,优雅的操作MongoDB使用MyBatisPlus的方式,优雅的操作MongoDB使用MyBatisPlus的方式,优雅的操作MongoDB...
Thinkphp是一个基于PHP的轻量级框架,它支持多种数据库,包括关系型数据库如MySQL,和非关系型数据库如MongoDB。...这些改动可以帮助Thinkphp在结合MongoDB使用时,为开发者提供更加强大和灵活的数据库查询能力。
本教程将引导你完成MongoDB的安装过程,并介绍其基本使用方法,适合在线教育初学者。 一、MongoDB概述 MongoDB是一款开源的文档型数据库,采用分布式文件存储,支持多种操作系统,包括Windows、Linux和macOS。它的...
一、MongoDB基础使用步骤 二、mongodb数据库的使用方式 三、命令行下MongoDB的使用链接地址 四、mongodb的GUI软件robo 3t的使用
什么是Mongodb Mongodb的优势 CRUD 索引 Mongodb管理 安装部署
MongoDB使用JSON格式的文档(BSON)作为其数据存储单位,这种格式易于理解和处理,尤其适合处理结构松散或半结构化数据。数据库由集合组成,集合又由文档构成。它支持丰富的查询语法,可以进行复杂的聚合操作,同时...
1. 数据模型:MongoDB使用文档型数据模型,文档是BSON格式的键值对,类似于JSON对象。 2. 数据库和集合:数据库是存储数据的逻辑单元,集合是数据库中的逻辑表格,不预先定义列。 3. 查询语言:MongoDB的查询语言...
2. **数据模型**:MongoDB使用文档型数据模型,文档是BSON(Binary JSON)格式,类似于JSON但包含更多数据类型。 3. **数据库操作**:包括创建、读取、更新和删除(CRUD)操作。例如,`db.collection.insertOne()`...
1. 高性能:MongoDB使用了高性能的存储引擎,可以快速地存储和检索数据。 2. 高可用:MongoDB支持高可用性,可以自动地将数据复制到多个节点上,从而确保数据的安全。 3. 可扩展:MongoDB支持水平扩展,可以根据需要...