forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplSetServersQueries.js
144 lines (126 loc) · 5.68 KB
/
replSetServersQueries.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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,
ReplSetServers = require('../lib/mongodb').ReplSetServers,
// BSON = require('../lib/mongodb').BSONPure;
BSON = require('../lib/mongodb').BSONNative;
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;
var port1 = 27018;
var port2 = 27019;
var server = new Server(host, port, {});
var server1 = new Server(host, port1, {});
var server2 = new Server(host, port2, {});
var servers = new Array();
servers[0] = server2;
servers[1] = server1;
servers[2] = server;
var replStat = new ReplSetServers(servers);
sys.puts("Connecting to " + host + ":" + port);
sys.puts("Connecting to " + host1 + ":" + port1);
sys.puts("Connecting to " + host2 + ":" + port2);
var db = new Db('node-mongo-examples', replStat, {native_parser:true});
db.open(function(err, db) {
db.dropDatabase(function() {
// Fetch the collection test
db.collection('test', function(err, collection) {
// Remove all records in collection if any
collection.remove(function(err, collection) {
// Insert three records
collection.insert([{'a':1}, {'a':2}, {'b':3}], function(docs) {
// Count the number of records
collection.count(function(err, count) {
sys.puts("There are " + count + " records.");
});
// Find all records. find() returns a cursor
collection.find(function(err, cursor) {
// Print each row, each document has an _id field added on insert
// to override the basic behaviour implement a primary key factory
// that provides a 12 byte value
sys.puts("Printing docs from Cursor Each")
cursor.each(function(err, doc) {
if(doc != null) sys.puts("Doc from Each " + sys.inspect(doc));
})
});
// Cursor has an to array method that reads in all the records to memory
collection.find(function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Printing docs from Array")
docs.forEach(function(doc) {
sys.puts("Doc from Array " + sys.inspect(doc));
});
});
});
// Different methods to access records (no printing of the results)
// Locate specific document by key
collection.find({'a':1}, function(err, cursor) {
cursor.nextObject(function(err, doc) {
sys.puts("Returned #1 documents");
});
});
// Find records sort by 'a', skip 1, limit 2 records
// Sort can be a single name, array, associate array or ordered hash
collection.find({}, {'skip':1, 'limit':1, 'sort':'a'}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
})
});
// Find all records with 'a' > 1, you can also use $lt, $gte or $lte
collection.find({'a':{'$gt':1}}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});
collection.find({'a':{'$gt':1, '$lte':3}}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});
// Find all records with 'a' in a set of values
collection.find({'a':{'$in':[1,2]}}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});
// Find by regexp
collection.find({'a':/[1|2]/}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});
// Print Query explanation
collection.find({'a':/[1|2]/}, function(err, cursor) {
cursor.explain(function(err, doc) {
sys.puts("-------------------------- Explanation");
sys.puts(sys.inspect(doc));
})
});
// Use a hint with a query, hint's can also be store in the collection
// and will be applied to each query done through the collection.
// Hint's can also be specified by query which will override the
// hint's associated with the collection
collection.createIndex('a', function(err, indexName) {
collection.hint = 'a';
// You will see a different explanation now that the hint was set
collection.find({'a':/[1|2]/}, function(err, cursor) {
cursor.explain(function(err, doc) {
sys.puts("-------------------------- Explanation");
sys.puts(sys.inspect(doc));
})
});
collection.find({'a':/[1|2]/}, {'hint':'a'}, function(err, cursor) {
cursor.explain(function(err, doc) {
sys.puts("-------------------------- Explanation");
sys.puts(sys.inspect(doc));
db.close();
})
});
});
});
});
});
});
});