~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to shell/mongo.js

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// mongo.js
 
2
 
 
3
// NOTE 'Mongo' may be defined here or in MongoJS.cpp.  Add code to init, not to this constructor.
 
4
if ( typeof Mongo == "undefined" ){
 
5
    Mongo = function( host ){
 
6
        this.init( host );
 
7
    }
 
8
}
 
9
 
 
10
if ( ! Mongo.prototype ){
 
11
    throw "Mongo.prototype not defined";
 
12
}
 
13
 
 
14
if ( ! Mongo.prototype.find )
 
15
    Mongo.prototype.find = function( ns , query , fields , limit , skip ){ throw "find not implemented"; }
 
16
if ( ! Mongo.prototype.insert )
 
17
    Mongo.prototype.insert = function( ns , obj ){ throw "insert not implemented"; }
 
18
if ( ! Mongo.prototype.remove )
 
19
    Mongo.prototype.remove = function( ns , pattern ){ throw "remove not implemented;" }
 
20
if ( ! Mongo.prototype.update )
 
21
    Mongo.prototype.update = function( ns , query , obj , upsert ){ throw "update not implemented;" }
 
22
 
 
23
if ( typeof mongoInject == "function" ){
 
24
    mongoInject( Mongo.prototype );
 
25
}
 
26
 
 
27
Mongo.prototype.setSlaveOk = function() {
 
28
    this.slaveOk = true;
 
29
}
 
30
 
 
31
Mongo.prototype.getDB = function( name ){
 
32
    return new DB( this , name );
 
33
}
 
34
 
 
35
Mongo.prototype.getDBs = function(){
 
36
    var res = this.getDB( "admin" ).runCommand( { "listDatabases" : 1 } );
 
37
    assert( res.ok == 1 , "listDatabases failed" );
 
38
    return res;
 
39
}
 
40
 
 
41
Mongo.prototype.getDBNames = function(){
 
42
    return this.getDBs().databases.map( 
 
43
        function(z){
 
44
            return z.name;
 
45
        }
 
46
    );
 
47
}
 
48
 
 
49
Mongo.prototype.getCollection = function(ns){
 
50
    var idx = ns.indexOf( "." );
 
51
    if ( idx < 0 ) 
 
52
        throw "need . in ns";
 
53
    var db = ns.substring( 0 , idx );
 
54
    var c = ns.substring( idx + 1 );
 
55
    return this.getDB( db ).getCollection( c );
 
56
}
 
57
 
 
58
Mongo.prototype.toString = function(){
 
59
    return "mongo connection to " + this.host;
 
60
}
 
61
 
 
62
connect = function( url , user , pass ){
 
63
    chatty( "connecting to: " + url )
 
64
 
 
65
    if ( user && ! pass )
 
66
        throw "you specified a user and not a password.  either you need a password, or you're using the old connect api";
 
67
 
 
68
    var idx = url.indexOf( "/" );
 
69
    
 
70
    var db;
 
71
    
 
72
    if ( idx < 0 )
 
73
        db = new Mongo().getDB( url );
 
74
    else 
 
75
        db = new Mongo( url.substring( 0 , idx ) ).getDB( url.substring( idx + 1 ) );
 
76
    
 
77
    if ( user && pass ){
 
78
        if ( ! db.auth( user , pass ) ){
 
79
            throw "couldn't login";
 
80
        }
 
81
    }
 
82
    
 
83
    return db;
 
84
}