Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@asya999
asya999 / explain1.js
Last active June 10, 2020 19:39
Explain aggregation 4 stages with group and slice
{
"stages" : [
{
"$cursor" : {
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "agg.scores",
"indexFilterSet" : false,
"parsedQuery" : {
"game" : {

Keybase proof

I hereby claim:

  • I am asya999 on github.
  • I am asya (https://keybase.io/asya) on keybase.
  • I have a public key ASCgrKHpwCuZFF_S0R44YJs3bhk_kvgi_MhNZQWpe_O0two

To claim this, I am signing this object:

@asya999
asya999 / rankArrayExample.js
Created July 19, 2017 19:43
Example using rankArray
> db.ranks.find()
{ "_id" : ObjectId("596f6e95b6a1aa8d363befeb"), "emp" : "xxx", "deptno" : 10 }
{ "_id" : ObjectId("596f6ea5b6a1aa8d363befec"), "emp" : "rrr", "deptno" : 10, "salary" : 10000 }
{ "_id" : ObjectId("596f6eacb6a1aa8d363befed"), "emp" : "fff", "deptno" : 10, "salary" : 40000 }
{ "_id" : ObjectId("596f6eb4b6a1aa8d363befee"), "emp" : "ddd", "deptno" : 10, "salary" : 40000 }
{ "_id" : ObjectId("596f6ebcb6a1aa8d363befef"), "emp" : "ccc", "deptno" : 10, "salary" : 50000 }
{ "_id" : ObjectId("596f6ec9b6a1aa8d363beff0"), "emp" : "bbb", "deptno" : 10, "salary" : 50000 }
{ "_id" : ObjectId("596f6ed7b6a1aa8d363beff1"), "emp" : "mmm", "deptno" : 11, "salary" : 5000 }
{ "_id" : ObjectId("596f6ee1b6a1aa8d363beff2"), "emp" : "nnn", "deptno" : 11, "salary" : 20000 }
{ "_id" : ObjectId("596f6eebb6a1aa8d363beff3"), "emp" : "kkk", "deptno" : 12, "salary" : 30000 }
@asya999
asya999 / rankArray.js
Created July 19, 2017 19:36
aggregation helper function
rankArray=function(inputArray, sortField="", dense=false) {
let suffix = "";
if (sortField!="") {
suffix = "."+sortField;
};
let orderVar = {};
if (dense) {
orderVar = {$cond:[
{$ne:["$$this"+suffix, "$$value.prevVal"]},
{$add:["$$value.order", 1]},
@asya999
asya999 / aggForFolderQuery.js
Created June 1, 2017 16:00
Aggregate for file collection
// variables used in query
_path="/path/to/my/folder";
basepath="folder";
collname="files";
// my example collection contents
db.getCollection(collname).find()
{ "_id" : 1, "name" : "", "type" : "folder", "createdAt" : ISODate("2017-05-02T04:00:00Z"), "updatedAt" : ISODate("2017-05-25T19:07:19.240Z") }
{ "_id" : 2, "name" : "path", "type" : "folder", "createdAt" : ISODate("2017-02-05T05:00:00Z"), "updatedAt" : ISODate("2017-05-25T19:07:19.240Z"), "parentId" : 1 }
{ "_id" : 3, "name" : "to", "type" : "folder", "createdAt" : ISODate("2017-03-01T05:00:00Z"), "updatedAt" : ISODate("2017-05-25T19:07:19.240Z"), "parentId" : 2 }
{ "_id" : 4, "name" : "my", "type" : "folder", "createdAt" : ISODate("2017-01-21T05:00:00Z"), "updatedAt" : ISODate("2017-05-25T19:07:19.240Z"), "parentId" : 3 }
> db.mixedTypes.aggregate({$project:{f1:1,typeF1:getTypes("$f1")}})
{ "_id" : ObjectId("55a5761b377e8e01062a12ab"), "f1" : 1, "typeF1" : "Number" }
{ "_id" : ObjectId("55a57622377e8e01062a12ac"), "f1" : "1.000", "typeF1" : "String" }
{ "_id" : ObjectId("55a57630377e8e01062a12ad"), "f1" : { "field" : 1 }, "typeF1" : "Subdocument" }
{ "_id" : ObjectId("55a5763d377e8e01062a12af"), "f1" : ObjectId("55a5763d377e8e01062a12ae"), "typeF1" : "ObjectId" }
{ "_id" : ObjectId("55a57644377e8e01062a12b0"), "f1" : true, "typeF1" : "True" }
{ "_id" : ObjectId("55a57647377e8e01062a12b1"), "f1" : false, "typeF1" : "False" }
{ "_id" : ObjectId("55a57651377e8e01062a12b2"), "f1" : ISODate("2015-07-14T20:51:29.324Z"), "typeF1" : "ISODate" }
{ "_id" : ObjectId("55a57659377e8e01062a12b3"), "f1" : null, "typeF1" : "Null" }
{ "_id" : ObjectId("55a57675377e8e01062a12b4"), "f1" : 1, "typeF1" : "Number" }
getTypes = function (field) {
var alltypes=[
{ t: "Regex", minv: Timestamp(2147483647,9999) },
{ t: "Timestamp", minv: Timestamp(0,0) },
{ t: "ISODate", minv: new Date(0,0,0) },
{ t: "True", minv: true },
{ t: "False", minv: false },
{ t: "ObjectId", minv: ObjectId("000000000000000000000000") },
{ t: "BinData", minv: BinData(0,"") },
{ t: "Array", minv: [] },
> db.mixedTypes.find()
{ "_id" : ObjectId("55a5761b377e8e01062a12ab"), "f1" : 1 }
{ "_id" : ObjectId("55a57622377e8e01062a12ac"), "f1" : "1.000" }
{ "_id" : ObjectId("55a57630377e8e01062a12ad"), "f1" : { "field" : 1 } }
{ "_id" : ObjectId("55a5763d377e8e01062a12af"), "f1" : ObjectId("55a5763d377e8e01062a12ae") }
{ "_id" : ObjectId("55a57644377e8e01062a12b0"), "f1" : true }
{ "_id" : ObjectId("55a57647377e8e01062a12b1"), "f1" : false }
{ "_id" : ObjectId("55a57651377e8e01062a12b2"), "f1" : ISODate("2015-07-14T20:51:29.324Z") }
{ "_id" : ObjectId("55a57659377e8e01062a12b3"), "f1" : null }
{ "_id" : ObjectId("55a57675377e8e01062a12b4"), "f1" : 1 }
db.createCollection( "email", { storageEngine: {
wiredTiger: { configString: "block_compressor=zlib" }}})
function cleanUpLong(max_running_secs) {
var currentOps = db.currentOp({
$and: [
{ op: "query" },
{ msg: { $exists: false } }, // not a sharding operation!
{ secs_running: { $gt: max_running_secs } },
{"ns": "backstage.action_loc" }
]
});
currentOps.inprog.forEach(function (op) {