Java constructs BSON data types

Source: Internet
Author: User

Java constructs BSON data types
  • Integer/Fu Fu points

    Java BSON constructs integer/Fu Fu point types
    {a:123, B:3.14}bsonobject obj = new Basicbsonobject (); Obj.put ("a", 123); Obj.put ("B", 3.14);
  • String

    Java BSON Construct String type
    {A: "hi"}bsonobject obj = new Basicbsonobject (); Obj.put ("A", "HI");
  • Empty type

    Java BSON constructed null type
    {A:null}bsonobject obj = new Basicbsonobject (); Obj.put ("a", null);
  • Object

    Java BSON Constructing nested object types
    {B:{a:1}}bsonobject subobj = new Basicbsonobject (); Subobj.put ("A", 1); Bsonobject obj = new Basicbsonobject (); Obj.put ("B", subobj);
  • Array

    Java BSON using Org.bson.types.BasicBSONList to construct array types
    {A:[0,1,2]}bsonobject obj = new Basicbsonobject (); Bsonobject arr = new Basicbsonlist () Arr.put ("0", 0), Arr.put ("1", 1); Arr.put ("2", 2); Obj.put ("A", arr);
  • Boolean

    Java BSON Constructs a Boolean type
    {a:true, B:false}bsonobject obj = new Basicbsonobject (); Obj.put ("A", true); Obj.put ("B", false);
  • Object ID

    Java BSON uses org.bson.types.ObjectId to generate the "_id" field content for each record. Java BSON 12 bytes of ObjectId and database concepts-the database-document-Object ID section describes the object ID slightly different, currently, Java ObjectId 12-byte content consists of three parts: 4 bytes Accurate to the second timestamp, 4-byte system (physical machine) label The sequence number starting with a random number of 4 bytes. By default, the database generates a unique object ID for each record that has a field named "_id."
    Bsonobject obj = new Basicbsonobject () ObjectId id1 = new ObjectId (); ObjectId id2 = new ObjectId ("53BB5667C5D061D6F579D0BB "); Obj.put (" _id ", ID1);
  • Regular expressions

    Java BSON uses Java.util.regex.Pattern to construct the regular expression data type.
    Bsonobject matcher = new Basicbsonobject (); Pattern obj = pattern.compile ("^2001", pattern.case_insensitive); Matcher.put ("Serial_num", obj); Bsonobject modifier = new Basicbsonobject ("$set", New Basicbsonobject ("Count", +); Cl.update (matcher, modifier, NULL );
    The above uses regular expressions to construct a matching condition that changes the contents of the Count field of a record with a serial number beginning with "2001" to "1000".Note: The above uses the Patten constructs the Bson Matcher, when uses the Matcher.tostring (), the content is:
    {"Serial_num": {"$options": "I", "$regex": "^2001"}}
    the same content can also be obtained by the following Bson constructs:
    Bsonobject matcher2 = new Basicbsonobject (); Bsonobject obj2 = new Basicbsonobject () obj2.put ("$regex", "^2001"), Obj2.put ("$options", "I"), Matcher2.put ("Serial_ Num ", OBJ2);
    However, the data type of the Matcher2 constructed by the latter is a normal object nesting type, not a regular expression type.
  • Date

    Java BSON uses java.util.Date to construct date types.
    Bsonobject obj = new Basicbsonobject ();D ate now = new Date (); Obj.put ("Date", now);
  • Binary

    Java BSON uses org.bson.types.Binary to construct a binary type.
    Bsonobject obj = new Basicbsonobject (); String str = "Hello world"; byte[] arr = str.getbytes (); Binary bindata = new binary (arr); Obj.put ("Bindata", bindata);
  • Time stamp

    Java BSON uses Org.bson.types.BSONTimestamp to construct timestamp types.
    int sec = 1404189030; 2014-07-01 12:30:30bsonobject obj = new Basicbsonobject (); Bsontimestamp ts = new Bsontimestamp (sec, 0); Obj.put ("timestamp", TS);

Java constructs BSON data types

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.