The idea of automatically creating collections by doing an INSERT has not yet been implemented.
CREATE COLLECTION abc;
CREATE COLLECTION def;
|
INSERT INTO
The content can be any valid JSON string - not just a JSON object, but any valid JSON. Note that (as with JavaScript) we do not require the double-quotes around the field labels of an object on input. However, the double-quotes will be inserted upon output.
INSERT INTO abc VALUE 1234 ;
INSERT INTO abc VALUE 3.141592653 ;
INSERT INTO abc VALUE "This is a string" ;
INSERT INTO abc VALUE [ "this" , "is" , "an" , "array" ];
INSERT INTO abc VALUE { type: "message" , content: "This is an object" };
INSERT INTO abc VALUE {
type: "nested" ,
content: {
content: "nested object" ,
x: 1 ,
y: {str: "hi" , str2: "there" },
z: true
}
};
|
SELECT FROM
Query the whole table.
Query Results
1234
3.141592653
"This is a string"
[ "this" , "is" , "an" , "array" ]
{ "type" : "message" , "content" : "This is an object" }
{ "type" : "nested" , "content" :{ "content" : "nested object" , "x" : 1 , "y" :{ "str" : "hi" , "str2" : "there" }, "z" : true }}
|
By default, the entire document is returned. But you can construct an alternative document in between the SELECT and FROM keywords.
SELECT { x:abc.type, y:abc.content.x, z:abc.content.x+ 50 } FROM abc;
|
Query Results
{ "x" : null , "y" : null , "z" : 0 }
{ "x" : null , "y" : null , "z" : 0 }
{ "x" : null , "y" : null , "z" : 0 }
{ "x" : null , "y" : null , "z" : 0 }
{ "x" : "message" , "y" : null , "z" : 0 }
{ "x" : "nested" , "y" : 1 , "z" : 51 }
|
SELECT FROM [collection] WHERE
You can of course put a WHERE clause to restrict the search.
SELECT FROM abc WHERE abc.type== "message" ;
|
Query Results
{ "type" : "message" , "content" : "This is an object" }
|
DELETE FROM
You can delete a single entry:
DELETE FROM abc WHERE abc== 1234 ;
SELECT FROM abc;
|
Query Results
3.141592653
"This is a string"
[ "this" , "is" , "an" , "array" ]
{ "type" : "message" , "content" : "This is an object" }
{ "type" : "nested" , "content" :{ "content" : "nested object" , "x" : 1 , "y" :{ "str" : "hi" , "str2" : "there" }, "z" : true }}
|
Or delete everything all at once:
DELETE FROM abc;
SELECT FROM abc;
|
The WHERE clause processing is still just a skeleton. Real soon now, you'll be able to use AND and OR with complex expressions in the WHERE clause in order to delete varying amounts of content.
UPSERT
Here's an example of an UPSERT: Incrementing a counter on a webpage.
UPDATE abc SET abc.n=abc.n+ 1 WHERE abc.page== "/page/one"
ELSE INSERT {page: "/page/one" , n: 1 , create_time: 1234567 };
SELECT FROM abc;
|
Query Results
{ "page" : "/page/one" , "n" : 1 , "create_time" : 1234567 }
|
UPDATE abc SET abc.n=abc.n+ 1 WHERE abc.page== "/page/one"
ELSE INSERT {page: "/page/one" , n: 1 , create_time: 1234567 };
SELECT FROM abc;
|
Query Results
{ "page" : "/page/one" , "n" : 2 , "create_time" : 1234567 }
|
UPDATE abc SET abc.n=abc.n+ 1 WHERE abc.page== "/page/one"
ELSE INSERT {page: "/page/one" , n: 1 , create_time: 1234567 };
SELECT FROM abc;
|
Query Results
{ "page" : "/page/one" , "n" : 3 , "create_time" : 1234567 }
|
UPDATE (adding new fields)
The final demonstration for today is the ability to add new fields to an object using UPDATE:
UPDATE abc SET abc.x = 54321 , abc.y = { this : "is" , a: "test" };
SELECT FROM abc;
|
Query Results
{ "page" : "/page/one" , "n" : 3 , "create_time" : 1234567 , "x" : 54321.00000000001 , "y" :{ "this" : "is" , "a" : "test" }}
|