楼主: kychan
11559 63

【2015新书】JavaScript JSON Cookbook   [推广有奖]

51楼
xiaohulu99 发表于 2015-8-16 00:57:24

52楼
sunyiping 发表于 2015-8-16 04:14:27
学习学习。

53楼
casbbyli 发表于 2015-8-16 08:15:13
xie xie

54楼
akta 发表于 2015-8-17 13:12:11
kankan

55楼
andttrip 发表于 2015-9-3 17:52:20
kankankan

56楼
ReneeBK 发表于 2015-9-3 20:55:42

57楼
Lisrelchen 发表于 2015-10-16 10:21:30

58楼
中国文学 发表于 2016-6-26 23:17:09

59楼
Reader's 发表于 2017-7-23 00:50:51
  1. Connecting to a MongoDB database using Node.js

  2. Before your Node.js application can do anything with a MongoDB instance, it must connect to it over the network.

  3. How to do it...

  4. The Node.js drivers for MongoDB contain all of the necessary network code to establish and break connections with MongoDB running on your local or remote machine.

  5. You need to include a reference to the native driver in your code and specify the URL of the database to connect to.

  6. Here's a simple example that connects to the database and promptly disconnects:

  7. var mongo = require('mongodb').MongoClient;

  8. var url = 'mongodb://localhost:27017/test';

  9. mongo.connect(url, function(error, db) {
  10.   console.log("mongo.connect returned " + error);
  11.   db.close();
  12. });
  13. Let's break this down line by line.
复制代码

60楼
Reader's 发表于 2017-7-23 00:52:48
  1. Creating a document in MongoDB using Node.js

  2. The MongoDB database organizes its documents in collections, which are typically groups of documents that are related in some way (such as representing the same kinds of information). Because of this, your primary interface to documents is through a collection. Let's see how to get a collection and add a document to it.

  3. Tip

  4. A collection is a little like a table in relational databases, but there's no imposition that all documents in a collection have the same fields or the same types for each field. Think of it as an abstraction you can use to group similar kinds of documents.

  5. How to do it...

  6. Here's a function that inserts two static items into the collection named documents in our test database, which we put in its own file and run using Node.js:

  7. var mongo = require('mongodb').MongoClient;

  8. var url = 'mongodb://localhost:27017/test';

  9. var insert = function(collection, callback) {
  10.   var documents =
  11.     [{
  12.         call: 'kf6gpe-7', lat: 37.0, lng: -122.0
  13.       },
  14.       {
  15.         call: 'kf6gpe-9', lat: 38.0, lng: -123.0
  16.       }];
  17.   // Insert some documents
  18.   collection.insert(documents,
  19.     function(error, result) {
  20.       console.log('Inserted ' +result.length + ' documents ' +
  21.         'with result: ');
  22.       console.log(result);
  23.       callback(result);
  24.   });
  25. };

  26. mongo.connect(url, function(error, db) {
  27.   console.log('mongo.connect returned ' + error);

  28.   // Get the documents collection
  29.   var collection = db.collection('documents');
  30.   insert(collection, function(result) {
  31.     db.close();
  32.   });
  33. });
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-10-1 21:06