Skip to content

Instantly share code, notes, and snippets.

@osroca
Last active September 2, 2016 16:44

Revisions

  1. osroca revised this gist Mar 11, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    From https://education.10gen.com/courses/10gen/M101/2012_Fall/wiki/how-embed-javascript-libraries-into-mongo-shell/_edit/

    There is obviously no `<script src="..">`; in mongo shell, nor is there an "include" or "run" command.

    Let's say, I'd like to embed the follwing libraries:
  2. osroca created this gist Dec 18, 2012.
    53 changes: 53 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    There is obviously no `<script src="..">`; in mongo shell, nor is there an "include" or "run" command.

    Let's say, I'd like to embed the follwing libraries:

    underscore.js
    xdate.js
    xdate.i18n.js

    I put theses files in a directory and rename them to make sure to load them in correct order (and make sure that they are utf-8 encoded!):

    i01-underscore.js
    i11-xdate.js
    i12-xdate.i18n.js

    now, start mongo shell (from that directory):

    mongo [db] --shell i*.js

    note: you may give a db name to connect to **before** the --shell, and don't use input redirection (less than) before the files.

    **now, you can do things like this:**

    ronald@ronald:~/m0101/libs_for_shell$ mongo --shell i*.js
    MongoDB shell version: 2.2.1
    connecting to: test
    type "help" for help
    loading file: i01-underscore-min.js
    loading file: i11-xdate.js
    loading file: i12-xdate.i18n.js
    > use blog
    switched to db blog
    > doc = db.posts.findOne()
    {
    "_id" : ObjectId("50a0e4b18cde4b5cd1000001"),
    "body" : "this is the first test",
    "permalink" : "test_number_one",
    "author" : "ronald",
    "tags" : [
    "mongodb",
    "test"
    ],
    "date" : ISODate("2012-11-12T11:59:45.048Z"),
    "title" : "test number one"
    }
    > _.keys(doc)
    [ "_id", "body", "permalink", "author", "tags", "date", "title" ]
    > new XDate(doc.date).toString("dd MMMM yyyy","pt")
    12 Novembro 2012
    > new XDate(doc.date).addWeeks(2).toString("MMMM, dd yyyy")
    November, 26 2012
    >