This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION public.json_append(data json, insert_data json) | |
RETURNS json | |
IMMUTABLE | |
LANGUAGE sql | |
AS $$ | |
SELECT ('{'||string_agg(to_json(key)||':'||value, ',')||'}')::json | |
FROM ( | |
SELECT * FROM json_each(data) | |
UNION ALL | |
SELECT * FROM json_each(insert_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// data density example | |
// shows how storing/sorting data by key can reduce I/O drastically in MongoDB | |
// diskloc gives file # and offset of a given document, divide by 512 for block # | |
// 2011 kcg | |
// start clean | |
db.disktest_noorg.drop(); | |
db.disktest_org.drop(); | |
// create some random data in non userid dense form |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.disktest_noorg.ensureIndex({"userid":-1}); | |
> db.disktest_org.ensureIndex({"userid":-1}); | |
> db.disktest_noorg.find().sort({userid:-1}) | |
{ "_id" : ObjectId("4dd2d82b6a2e502b3043ef33"), "userid" : 49999, "imageid" : 20, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "79357fb65ba7b87f2632dfe8e098400c" } | |
{ "_id" : ObjectId("4dd2d82e6a2e502b30442186"), "userid" : 49999, "imageid" : 14, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "43a98d693fc469b323f2c9033176cd97" } | |
{ "_id" : ObjectId("4dd2d8316a2e502b30448652"), "userid" : 49999, "imageid" : 18, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "810c9ca8b61eff972e0d514191874f22" } | |
{ "_id" : ObjectId("4dd2d82a6a2e502b3043d4d4"), "userid" : 49996, "imageid" : 39, "img" : "www.kennygorman.com/foo.jpg", "title" : "This is a sample title", "data" : "e864723614597f09c6d09469aa3e3235" } | |
{ "_id" : ObjectId("4dd2d82b6a2e502b3043efcd"), "userid" : 49995, "ima |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* MongoDB cheat sheet */ | |
// replication lag via serverStatus() | |
db._adminCommand( { serverStatus : 1 , repl ; 2 } ) | |
// stats | |
db.stats() | |
db.foo.stats() | |
// size of BSON of some query |