GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fullstack-javascript/11-db-server/collections.js at master · graymd/fullstack-javascript

Expand file tree

/

collections.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
51 lines (43 loc) · 1.47 KB
/

collections.js

Copy path

File metadata and controls

51 lines (43 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
Rapid Prototyping with JS is a JavaScript and Node.js book that will teach you how to build mobile and web apps fast. — Read more at
http://rapidprototypingwithjs.com.
*/
var mongodb = require('mongodb');
var url = require('url');
var log = console.log;
var dbUri = process.env.MONGOHQ_URL || 'mongodb://localhost:27017/test';
var connectionUri = url.parse(dbUri);
var dbName = connectionUri.pathname.replace(/^\//, '');
mongodb.Db.connect(dbUri, function(error, client) {
if (error) throw error;
client.collectionNames(function(error, names){
if(error) throw error;
//output all collection names
log("Collections");
log("===========");
var lastCollection = null;
names.forEach(function(colData){
var colName = colData.name.replace(dbName + ".", '')
log(colName);
lastCollection = colName;
});
if (!lastCollection) return;
var collection = new mongodb.Collection(client, lastCollection);
log("\nDocuments in " + lastCollection);
var documents = collection.find({}, {limit:5});
//output a count of all documents found
documents.count(function(error, count){
log(" " + count + " documents(s) found");
log("====================");
// output the first 5 documents
documents.toArray(function(error, docs) {
if(error) throw error;
docs.forEach(function(doc){
log(doc);
});
// close the connection
client.close();
});
});
});
});
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.