Original address: http://keben1983.blog.163.com/blog/static/143638081201010591157929/
This document is translated from the [mongodb-docs-2010-10-24.pdf] [Java Language Center] section of the document and is organized according to your own understanding.
Hope to give a friend like me to start contact a little help, but also make a memo, because is just learning, many of these features are currently not used, in the future, if what the function is not clear, you can directly consult the document.
MongoDB Java Driver Simple operation
First, Java-driven consistency
MongoDB Java driver is thread-safe, for general applications, as long as a MONGO instance, MONGO has a built-in connection pool (pool size defaults to 10).
For environments with lots of writing and reading, to ensure that the same db is used in a session, we can guarantee consistency in the following ways:
DB mdb = mongo.getdb (' dbname ');
Mdb.requeststart ();
//
Business code
//
Mdb.requestdone ();
DB and Dbcollection are absolutely thread-safe, they are cached, so the same object may be taken in the application.
Ii. Save/Find Objects (DBObject)
The Java driver provides a dbobject interface for us to save objects to the database.
Define the objects you want to save:
public class Tweet implements DBObject {
/** ...... */
}
Then we can use the object:
Tweet = new Tweet ();
Tweet.put ("User", userId);
Tweet.put ("message", message);
Tweet.put ("Date", new Date ());
Collection.insert (tweet);
When you query from the database, the results are automatically converted to DBObject objects, and we can convert to our own type:
Collection.setobjectclass (Tweet);
Tweet Mytweet = (tweet) collection.findone ();
Third, create a connection
Mongo m = new Mongo ();
Mongo m = new Mongo ("localhost");
Mongo m = new Mongo ("localhost", 27017);
DB db = M.getdb ("MyDB");
Note: In fact, the MONGO instance represents a database connection pool, and even in a multi-threaded environment, a MONGO instance is sufficient for us.
MongoDB supports indexing, and adding an index to a dbcollection is simple, you just point to the field you want to create an index on, and then indicate whether it's ascending (1) or Descending (-1), such as creating an ascending index on "i".
Coll.createindex (New Basicdbobject ("I", 1)); 1 represents ascending
Query index
We can query all the indexes:
list<dbobject> list = Coll.getindexinfo ();
for (DBObject index:list) {
SYSTEM.OUT.PRINTLN (index);
}
The output of the console looks similar to the following:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.