A MongoDB instance is running in MongoDB.

Source: Internet
Author: User
Tags install mongodb

There is no way in the world, and when there are more people, the road will become. Why can't I create a MongoDB on ipvs?

After taking a series of actions in NoSQL, The Postgres community is not waiting. Postgres has been making progress: Integrating JSON and PLV8. PLV8 into the V8 Javascript Engine. JSON operations are simpler (verification required ).

Preparations before you start:

  • Postgres 9.2 + (as of this blog entry, 9.2 is in beta)-http://www.postgresql.org/ftp/source/

  • V8 https://github.com/v8/v8

  • PLV8 http://code.google.com/p/plv8js/wiki/PLV8

The minimum level of MongoDB is set. The set can be expressed in tables:

Create table some_collection (
Some_collection_id serial not null primary key,
Data JSON
);

JSON of the struct type is saved in the s table, which is easy to use (as shown in the following figure ).

The following describes how to automatically create a set and save it in the set table:

Create table collection (
Collection_id serial not null primary key,
Name VARCHAR
);

-- Make sure the name is unique
Create unique index idx_collection_constraint ON collection (name );

Once the table is created, you can use the stored procedure to automatically create a set. The method is to create a table first and then insert the sequence of table creation.

Create or replace function create_collection (collection varchar) RETURNS
Boolean AS $
Var plan1 = plv8.prepare ('insert INTO collection (name) VALUES ($1) ', ['varchar']);
Var plan2 = plv8.prepare ('create TABLE col _ '+ collection +
'(Col _' + collection + '_ id int not null primary key, data JSON )');
Var plan3 = plv8.prepare ('create SEQUENCE seq_col _ '+ collection );

Var ret;

Try {
Plv8.subtransaction (function (){
Plan1.execute ([collection]);
Plan2.execute ([]);
Plan3.execute ([]);

Ret = true;
});
} Catch (err ){
Ret = false;
}

Plan1.free ();
Plan2.free ();
Plan3.free ();

Return ret;
$ LANGUAGE plv8 immutable strict;

With the stored procedure, it is much easier:

SELECT create_collection ('My _ collect ');

To solve the problem of centralized storage, let's take a look at MongoDB data parsing. MongoDB completes this operation through the point annotation method:

Create or replace function find_in_obj (data json, key varchar) RETURNS
Varchar as $
Var obj = JSON. parse (data );
Var parts = key. split ('.');

Var part = parts. shift ();
While (part & (obj = obj [part])! = Undefined ){
Part = parts. shift ();
}

// This will either be the value, or undefined
Return obj;
$ LANGUAGE plv8 STRICT;

The above function returns VARCHAR and does not apply to all cases, but it is useful for strings:

SELECT data
FROM col_my_collection
WHERE find_in_obj (data, 'some. element') = 'something cool'

In addition to string comparison, MongoDB also provides comparison of numeric types and the keyword exists. Below are different implementations of the find_in_obj () method:

Create or replace function find_in_obj_int (data json, key varchar) RETURNS
Int as $
Var obj = JSON. parse (data );
Var parts = key. split ('.');

Var part = parts. shift ();
While (part & (obj = obj [part])! = Undefined ){
Part = parts. shift ();
}

Return Number (obj );
$ LANGUAGE plv8 STRICT;

Create or replace function find_in_obj_exists (data json, key varchar) RETURNS
Boolean as $
Var obj = JSON. parse (data );
Var parts = key. split ('.');

Var part = parts. shift ();
While (part & (obj = obj [part])! = Undefined ){
Part = parts. shift ();
}

Return (obj = undefined? 'F': 'T ');
$ LANGUAGE plv8 STRICT;

The next step is data query. Use existing materials to implement the find () method.

CentOS compilation and installation of MongoDB

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

 

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.