借助nodejs解析加密字符串 node安装库较python方便
const node_modules_path = '../node_modules/' // crypto-js - npm https://www.npmjs.com/package/crypto-js
const CryptoJS = require(node_modules_path + 'crypto-js')
// Encrypt
const ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123')
// Decrypt
const bytes = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123')
const plaintext = bytes.toString(CryptoJS.enc.Utf8)
console.log(plaintext) const mongoCfg = {
uri: 'mongodb://hbaseU:123@192.168.3.103:27017/hbase',
dbName: 'hbase'
}
const MongoClient = require(node_modules_path + 'mongodb').MongoClient
const assert = require(node_modules_path + 'assert') // Use connect method to connect to the server
MongoClient.connect(mongoCfg.uri, function (err, client) {
assert.equal(null, err)
console.log('Connected successfully to server')
const db = client.db(mongoCfg.dbName)
insertDocuments(db, function () {
console.log('cb..')
})
client.close()
}) const insertDocuments = function (db, callback) {
// Get the documents collection
const collection = db.collection('documents')
// Insert some documents
collection.insertMany([
{a: 1}, {a: 2}, {a: 3}
], function (err, result) {
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the collection");
callback(result);
})
}
Description
The official MongoDB driver for Node.js. Provides a high-level API on top of mongodb-corethat is meant for end users.
NOTE: v3.x was recently released with breaking API changes. You can find a list of changeshere.
MongoDB Node.JS Driver
what | where |
---|---|
documentation | http://mongodb.github.io/node-mongodb-native |
api-doc | http://mongodb.github.io/node-mongodb-native/3.1/api |
source | https://github.com/mongodb/node-mongodb-native |
mongodb | http://www.mongodb.org |
Bugs / Feature Requests
Think you’ve found a bug? Want to see a new feature in node-mongodb-native
? Please open a case in our issue management tool, JIRA:
- Create an account and login jira.mongodb.org.
- Navigate to the NODE project jira.mongodb.org/browse/NODE.
- Click Create Issue - Please provide as much information as possible about the issue type and how to reproduce it.
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are public.
Questions and Bug Reports
- Mailing List: groups.google.com/forum/#!forum/node-mongodb-native
- JIRA: jira.mongodb.org
Change Log
Change history can be found in HISTORY.md
.
Installation
The recommended way to get started using the Node.js 3.0 driver is by using the npm
(Node Package Manager) to install the dependency in your project.
MongoDB Driver
Given that you have created your own project using npm init
we install the MongoDB driver and its dependencies by executing the following npm
command.
This will download the MongoDB driver and add a dependency entry in yourpackage.json
file.
You can also use the Yarn package manager.
Troubleshooting
The MongoDB driver depends on several other packages. These are:
The kerberos
package is a C++ extension that requires a build environment to be installed on your system. You must be able to build Node.js itself in order to compile and install thekerberos
module. Furthermore, the kerberos
module requires the MIT Kerberos package to correctly compile on UNIX operating systems. Consult your UNIX operation system package manager for what libraries to install.
Windows already contains the SSPI API used for Kerberos authentication. However, you will need to install a full compiler tool chain using Visual Studio C++ to correctly install the Kerberos extension.
Diagnosing on UNIX
If you don’t have the build-essentials, this module won’t build. In the case of Linux, you will need gcc, g++, Node.js with all the headers and Python. The easiest way to figure out what’s missing is by trying to build the Kerberos project. You can do this by performing the following steps.
If all the steps complete, you have the right toolchain installed. If you get the error "node-gyp not found," you need to install node-gyp
globally:
If it correctly compiles and runs the tests you are golden. We can now try to install themongod
driver by performing the following command.
If it still fails the next step is to examine the npm log. Rerun the command but in this case in verbose mode.
This will print out all the steps npm is performing while trying to install the module.
Diagnosing on Windows
A compiler tool chain known to work for compiling kerberos
on Windows is the following.
- Visual Studio C++ 2010 (do not use higher versions)
- Windows 7 64bit SDK
- Python 2.7 or higher
Open the Visual Studio command prompt. Ensure node.exe
is in your path and installnode-gyp
.
Next, you will have to build the project manually to test it. Clone the repo, install dependencies and rebuild:
This should rebuild the driver successfully if you have everything set up correctly.
Other possible issues
Your Python installation might be hosed making gyp break. Test your deployment environment first by trying to build Node.js itself on the server in question, as this should unearth any issues with broken packages (and there are a lot of broken packages out there).
Another tip is to ensure your user has write permission to wherever the Node.js modules are being installed.
Quick Start
This guide will show you how to set up a simple application using Node.js and MongoDB. Its scope is only how to set up the driver and perform the simple CRUD operations. For more in-depth coverage, see the tutorials.
Create the package.json
file
First, create a directory where your application will live.
Enter the following command and answer the questions to create the initial structure for your new project:
Next, install the driver dependency.
You should see NPM download a lot of files. Once it's done you'll find all the downloaded packages under the node_modules directory.
Start a MongoDB Server
For complete MongoDB installation instructions, see the manual.
- Download the right MongoDB version from MongoDB
- Create a database directory (in this case under /data).
- Install and start a
mongod
process.
You should see the mongod process start up and print some status information.
Connect to MongoDB
Create a new app.js file and add the following code to try out some basic CRUD operations using the MongoDB driver.
Add code to connect to the server and the database myproject:
Run your app from the command line with:
The application should print Connected successfully to server to the console.
Insert a Document
Add to app.js the following function which uses the insertMany method to add three documents to the documents collection.
The insert command returns an object with the following fields:
- result Contains the result document from MongoDB
- ops Contains the documents inserted with added _id fields
- connection Contains the connection used to perform the insert
Add the following code to call the insertDocuments function:
Run the updated app.js file:
The operation returns the following output:
Find All Documents
Add a query that returns all the documents.
This query returns all the documents in the documents collection. Add the findDocumentmethod to the MongoClient.connect callback:
Find Documents with a Query Filter
Add a query filter to find only documents which meet the query criteria.
Only the documents which match 'a' : 3
should be returned.
Update a document
The following operation updates a document in the documents collection.
The method updates the first document where the field a is equal to 2 by adding a new fieldb to the document set to 1. Next, update the callback function from MongoClient.connect to include the update method.
Remove a document
Remove the document where the field a is equal to 3.
Add the new method to the MongoClient.connect callback function.
Index a Collection
Indexes can improve your application's performance. The following function creates an index on the a field in the documents collection.
Add the indexCollection
method to your app:
For more detailed information, see the tutorials.
Next Steps
install
npm i mongodb
weekly downloads
710,340.000
version
3.1.4
license
Apache-2.0
open issues
pull requests
homepage
repository
last publish
9 days ago
最新文章
- Servlet的生命周期及工作原理
- 《Entity Framework 6 Recipes》中文翻译系列 (12) -----第三章 查询之使用SQL语句
- Xen启动过程分析(还是分享过来吧,找了好长时间)
- PP 创建BOM
- [转]Oracle 修改或者删除临时表 ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引
- 自然数从1到n之间,有多少个数字含有1
- 【笔记】WPF之模板控件应用
- hdu4223(dp)
- web开发性能优化---项目架构篇
- Java之进程与线程
- SpringMvc返回报文形式的控制-验证方法: JSON or HTML or XML
- html-webpack-template, 一个更好的html web service插件
- 本文讲述下windows下使用rsync备份数据
- zxing源码编译与运行
- Cesium学习1:如何在本机的Apache tomcat9.0.8服务器中打开cesium的index.html页面
- USB学习笔记连载(二十一):CY7C68013A进行数据传输(一)
- ext4.2常用的几种弹框
- JS代码判断浏览器类型以及版本
- C#将图片存放到SQL SERVER数据库中的方法
- laravel用redis保存session遇到的坑,没报错,但redis-cli就是查不到