forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.js
66 lines (53 loc) · 2.47 KB
/
index_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
GLOBAL.DEBUG = true;
sys = require("sys");
test = require("assert");
var Db = require('../lib/mongodb').Db,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
// BSON = require('../lib/mongodb').BSONPure;
BSON = require('../lib/mongodb').BSONNative;
var mongo = require('../lib/mongodb'),
Integer = require('../lib/mongodb/goog/math/integer').Integer;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
sys.puts(">> Connecting to " + host + ":" + port);
var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
db.open(function(err, db) {
sys.puts(">> Dropping collection test");
db.dropCollection('test', function(err, result) {
sys.puts("dropped: " + sys.inspect(result));
});
sys.puts(">> Creating collection test");
db.collection('test', function(err, collection) {
sys.puts("created: " + sys.inspect(collection));
var objectCount = 100;
var objects = [];
var messages = ["hola", "hello", "aloha", "ciao"];
sys.puts(">> Generate test data");
for(var i = 0; i < objectCount; i++) {
objects.push({'number':i, 'rndm':((5*Math.random()) + 1), 'msg':messages[Integer.fromNumber((4*Math.random())).toInt()]})
}
sys.puts("generated");
sys.puts(">> Inserting data (" + objects.length + ")");
collection.insert(objects);
sys.puts("inserted");
sys.puts(">> Creating index")
collection.createIndex([['all'], ['_id', 1], ['number', 1], ['rndm', 1], ['msg', 1]], function(err, indexName) {
sys.puts("created index: " + indexName);
sys.puts(">> Gathering index information");
collection.indexInformation(function(err, doc) {
sys.puts("indexInformation: " + sys.inspect(doc));
sys.puts(">> Dropping index");
collection.dropIndex('all_1__id_1_number_1_rndm_1_msg_1', function(err, result) {
sys.puts("dropped: " + sys.inspect(result));
sys.puts(">> Gathering index information");
collection.indexInformation(function(err, doc) {
sys.puts("indexInformation: " + sys.inspect(doc));
sys.puts(">> Closing connection");
db.close();
});
});
});
});
});
});