Java data types and test examples supported by MongoDB _java

Source: Internet
Author: User
Tags date now mongodb

The Java data types for MongoDB are:

Object IDs Auto ID
Regular Expressions Regular Expression Search records
Dates/times time
Database References Data Infrastructure
Binary Data Binary Stream
Timestamp Data time Stamp
Code Data Codes
Embedded Documents Embedded Document
Arrays array type

Detailed reference to the original English http://docs.mongodb.org/ecosystem/drivers/java-types/

The MongoDB data type for the following test base.

Copy Code code as follows:

Package Com.javaer.mongo;

Import java.net.UnknownHostException;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.regex.Pattern;

Import Org.bson.types.ObjectId;

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.DBRef;
Import Com.mongodb.Mongo;

public class Javatypes {

/**
* @param args
* @throws unknownhostexception
*/
public static void Main (string[] args) throws Unknownhostexception {
Base ID, automatically adding structure similar to 503991D90364B431CDC798E2
ObjectId id = new ObjectId ();
SYSTEM.OUT.PRINTLN (ID);

id = new ObjectId ();
SYSTEM.OUT.PRINTLN (ID);
5039924c0364f4a124555e42
5039924c0364f4a124555e43 continuous use, automatically increase, otherwise randomly generated

Mongo m = new Mongo ();
DB db = M.getdb ("Yuexiaosheng");
Dbcollection coll = db.getcollection ("mycollection");


Using regular expressions from a standard library, you can search for records in your data. Similar to the MySQL condition where title like '%name% '
Pattern John = Pattern.compile (". *?jack*.?", pattern.case_insensitive);
Basicdbobject query = new Basicdbobject ("title", John);

dbcursor cursor = coll.find (query);
try {
while (Cursor.hasnext ()) {
System.out.println (Cursor.next ());
}
finally {
Cursor.close ();
}

Type of time in MongoDB
Date now = new Date ();
Basicdbobject time = new Basicdbobject ("TS", now);
Coll.save (time);
Inserted record {"_id": ObjectId ("503993b20364129f6625d349"), "TS": Isodate ("2012-08-26t03:10:42.146z")}

The array structure in the MongoDB.
ArrayList x = new ArrayList ();
X.add (1);
X.add (2);
X.add (New Basicdbobject ("foo", "Bar"));
X.add (4);

Basicdbobject doc = new Basicdbobject ("X", x);
Coll.save (DOC);
Inserted record {"_id": ObjectId ("503994500364bf9957b271a6"), "X": [1, 2, {"foo": "Bar"}, 4]}


Store MongoDB data to the database
Dbref addressref = new Dbref (db, "Foo.bar", id);
DBObject address = Addressref.fetch ();

DBObject person = Basicdbobjectbuilder.start ()
. Add ("Name", "Fred")
. Add ("Address", Addressref)
. get ();
Coll.save (person);
Inserted data records
{"_id": ObjectId ("503995390364fcf1b54905b2"), "name": "Fred", "address": {"$ref": "Foo.bar", "$id": ObjectId ("503 995390364fcf1b54905af ")}}

DBObject fred = Coll.findone ();

Dbref addressobj = (dbref) fred.get ("Address");
System.out.println (Addressobj);
{"$ref": "Foo.bar", "$id": "5039965c03648efcd6281311

}

}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.