Archive for the ‘mongodb’ Category

nodejs: mongoose module and mongodb

July 27, 2017

The post shows how a nodejs program connects to mongoDB with mongoose module.

version

bash-3.2$ node -v
v8.1.4

init nodejs project

bash-3.2$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (myapp) 
version: (1.0.0) 
description: 
entry point: (index.js) app.js
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/chengyihe/workspace/node/myapp/package.json:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) 

install mongoose module
By default, npm install modules locally rather than globally. The files related to mongoose module is put under $(pwd)/node_modules directory.

bash-3.2$ npm install mongoose
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN myapp@1.0.0 No description
npm WARN myapp@1.0.0 No repository field.

+ mongoose@4.11.4
added 31 packages in 6.164s
bash-3.2$ ls -l node_modules/
total 0
drwxr-xr-x   82 chengyihe  staff   2788 Jul 27 07:20 async
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 bluebird
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 bson
drwxr-xr-x    6 chengyihe  staff    204 Jul 27 07:20 buffer-shims
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 core-util-is
drwxr-xr-x   15 chengyihe  staff    510 Jul 27 07:20 debug
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 es6-promise
drwxr-xr-x    9 chengyihe  staff    306 Jul 27 07:20 hooks-fixed
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 inherits
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 isarray
drwxr-xr-x   12 chengyihe  staff    408 Jul 27 07:20 kareem
drwxr-xr-x  640 chengyihe  staff  21760 Jul 27 07:20 lodash
drwxr-xr-x   17 chengyihe  staff    578 Jul 27 07:20 mongodb
drwxr-xr-x   23 chengyihe  staff    782 Jul 27 07:20 mongodb-core
drwxr-xr-x   18 chengyihe  staff    612 Jul 27 07:20 mongoose
drwxr-xr-x   16 chengyihe  staff    544 Jul 27 07:20 mpath
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 mpromise
drwxr-xr-x   12 chengyihe  staff    408 Jul 27 07:20 mquery
drwxr-xr-x    6 chengyihe  staff    204 Jul 27 07:20 ms
drwxr-xr-x   12 chengyihe  staff    408 Jul 27 07:20 muri
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 process-nextick-args
drwxr-xr-x   19 chengyihe  staff    646 Jul 27 07:20 readable-stream
drwxr-xr-x   11 chengyihe  staff    374 Jul 27 07:20 regexp-clone
drwxr-xr-x   10 chengyihe  staff    340 Jul 27 07:20 require_optional
drwxr-xr-x    6 chengyihe  staff    204 Jul 27 07:20 resolve-from
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 safe-buffer
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 semver
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 sliced
drwxr-xr-x    7 chengyihe  staff    238 Jul 27 07:20 string_decoder
drwxr-xr-x    8 chengyihe  staff    272 Jul 27 07:20 util-deprecate

mongodb client source code
The nodejs example loads mongoose module, connects to a mongoDB server, and then inserts a data satisfied a schema.

  • require(‘mongoose’): it loads mongoose module
  • mongoose.connect(): it connects to a database in a mongoDB server
  • Schema: specify the data structure of document in collection
  • Model: it corresponds to a collection in the connected mongoDB server. The first parameter specifies the collection name, the second one is a schema which specifies the format of documents within the collection
bash-3.2$ cat app.js 
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useMongoClient: true,})

var Schema = mongoose.Schema
var memberSchema = new Schema(
    {
        name: String
    }
)
var member = mongoose.model('member', memberSchema)
var person = new member({ name: 'chengyihe' })

person.save(function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('saved');
    }
});

output of running the mongodb client
Output shows data is saved in mongoDB server successfully.

bash-3.2$ node app.js
(node:54460) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
saved

use mongodb shell to check the result
It shows that the test database’s members collection has a document which is inserted by above example.

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings:
2017-07-26T20:43:13.356+0800 I CONTROL  [initandlisten]
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten]
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten]
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> db.kernels.drop()
true
> show collections
members
> db.members.find()
{ "_id" : ObjectId("597936a8cff68dd4bc0b4c71"), "name" : "chengyihe", "__v" : 0 }
> 

conclusion
The post uses a nodejs with mongoose module to insert data in a mongoDB server. Then, it query the data with mongoDB shell.

mongodb: find(), limit() and skip()

July 26, 2017

The post shows how to use limit() and skip() to partial shows data.

version

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017

show all documents

> db.member.find()
{ "_id" : ObjectId("5978cda6558e820054f7be9c"), "name" : "chengyihe" }
{ "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }
{ "_id" : ObjectId("5978cdaf558e820054f7be9e"), "name" : "max" }
{ "_id" : ObjectId("5978cdb2558e820054f7be9f"), "name" : "daniel" }

show the first two documents

> db.member.find().limit(2)
{ "_id" : ObjectId("5978cda6558e820054f7be9c"), "name" : "chengyihe" }
{ "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }

after skipping the first document, show the first two documents

> db.member.find().limit(2).skip(1)
{ "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }
{ "_id" : ObjectId("5978cdaf558e820054f7be9e"), "name" : "max" }

conclusion
The post shows some example to demonstrate the functionality of limit() and skip().

mongodb: find and projection

July 26, 2017

This post discusses find and projection in mongoDB.

version

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017

insert document into a collection
Add four documents into test.member collection.

> show dbs
admin  0.000GB
local  0.000GB
> db
test
> db.member.insert({name: 'chengyihe'})
WriteResult({ "nInserted" : 1 })
> db.member.insert({name: 'woody'})
WriteResult({ "nInserted" : 1 })
> db.member.insert({name: 'max'})
WriteResult({ "nInserted" : 1 })
> db.member.insert({name: 'daniel'})
WriteResult({ "nInserted" : 1 })

show documents in a collection with projection

  • db.member.find() list all documents with all fields.
  • db.member.find({},{_id: 0, name: 1}) list all documents with only name field.
  • > db.member.find()
    { "_id" : ObjectId("5978cda6558e820054f7be9c"), "name" : "chengyihe" }
    { "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }
    { "_id" : ObjectId("5978cdaf558e820054f7be9e"), "name" : "max" }
    { "_id" : ObjectId("5978cdb2558e820054f7be9f"), "name" : "daniel" }
    > db.member.find({},{_id: 0, name: 1})
    { "name" : "chengyihe" }
    { "name" : "woody" }
    { "name" : "max" }
    { "name" : "daniel" }
    

    conclusion
    The post shows how to use projection operator to show only some fields of satisfied documents.

mongodb: mongo shell and insert data

July 26, 2017

The post discusses using mongoDB shell to insert data.

software version

bash-3.2$ mongo
MongoDB shell version v3.4.6

connect to mongodb database

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings: 
2017-07-26T20:43:13.356+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

database, collection, and document

  • A mongoDB server has many databases.
  • A mongoDB database has many collections.
  • A mongoDB collection has many documents.
  • A mongoDB document is a JSON format data.

insert a document into test database’s member collection

  • The default database is test.
  • By default, the mongoDB server has two databases which have data.
  • The example inserts a document {‘name’: ‘chengyihe’} into member collection of test database.
  • WriteResult({ “nInserted” : 1 }) indicates that the insertion operation is success.
  • After inserting data into test database, “show dbs” also shows test database.
> db.getName()
test
> show dbs
admin  0.000GB
local  0.000GB
> db.member.insert({'name': 'chengyihe'})
WriteResult({ "nInserted" : 1 })
> show dbs
admin  0.000GB
local  0.000GB
test   0.000GB

list data in database

  • db.getName(): show current database name
  • show collections: list a collection in current database
  • db.member.find(): find all documents in member collection
> db.getName()
tes
> show collections
member
> db.member.find()
{ "_id" : ObjectId("5978953fe2861159432c64ed"), "name" : "chengyihe" }

conclusion
The post shows how to insert data into mongoDB database.

mongodb: mongod and mongo shell

July 26, 2017

The post discusses mongod and mongo shell.

software version

bash-3.2$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1510
bash-3.2$ mongod --version
db version v3.4.6
git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
OpenSSL version: OpenSSL 1.0.2l  25 May 2017
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64
bash-3.2$ mongo --version
MongoDB shell version v3.4.6
git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
OpenSSL version: OpenSSL 1.0.2l  25 May 2017
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

what is mongod
A server side mongoDB executable.

what is mongo shell
A client side mongoDB interactive shell.

run mongod as the the mongoDB server in localhost at port 27017
By default, the mongod runs the server at port 27017.

bash-3.2$ mongod -dbpath=$(pwd)/db
2017-07-26T20:43:12.449+0800 I CONTROL  [initandlisten] MongoDB starting : pid=51719 port=27017 dbpath=/Users/chengyihe/workspace/mongodb/db 64-bit host=nanshouoshisMBP
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten] db version v3.4.6
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2l  25 May 2017
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten] allocator: system
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten] modules: none
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten] build environment:
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-26T20:43:12.450+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-26T20:43:12.451+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/Users/chengyihe/workspace/mongodb/db" } }
2017-07-26T20:43:12.453+0800 I -        [initandlisten] Detected data files in /Users/chengyihe/workspace/mongodb/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-07-26T20:43:12.454+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-07-26T20:43:13.356+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-07-26T20:43:13.531+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/Users/chengyihe/workspace/mongodb/db/diagnostic.data'
2017-07-26T20:43:13.533+0800 I NETWORK  [thread1] waiting for connections on port 27017

run mongo shell as mongoDB client
By default, mongo shell connects to mongoDB at 127.0.0.1:27017.

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2017-07-26T20:43:13.356+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] 
2017-07-26T20:43:13.357+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>

List databases in mongoDB
Use “show dbs” to list all databases.

> show dbs
admin  0.000GB
local  0.000GB

conclusion
The post discusses how to use mongoDB shell to connects and send commands to mongoDB server.

mongodb: install mongodb in Mac OS X

July 26, 2017

The post discusses installing mongoDB in Mac OS X.

software version

bash-3.2$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1510

what is mongodb
A non-sql database.

install mongodb

bash-3.2$ brew update
bash-3.2$ brew install mongodb
Updating Homebrew...
==> Installing dependencies for mongodb: openssl
==> Installing mongodb dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2l.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2l.el_capitan.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
�  /usr/local/Cellar/openssl/1.0.2l: 1,709 files, 12.1MB
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.6.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.6.el_capitan.bottle.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
  brew services start mongodb
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
�  /usr/local/Cellar/mongodb/3.4.6: 18 files, 267.5MB

Run mongdbb in localhost
By default, mongod uses /data/db as database directory. If another place if preferred, then -dbpath flag could help assign another directory as mongoDB directory.

bash-3.2$ mkdir db
bash-3.2$ mongod -dbpath=$(pwd)/db
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] MongoDB starting : pid=51583 port=27017 dbpath=/Users/chengyihe/workspace/mongodb/db 64-bit host=nanshouoshisMBP
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] db version v3.4.6
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2l  25 May 2017
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] allocator: system
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] modules: none
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] build environment:
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-26T20:11:49.965+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/Users/chengyihe/workspace/mongodb/db" } }
2017-07-26T20:11:49.966+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-07-26T20:11:50.437+0800 I CONTROL  [initandlisten] 
2017-07-26T20:11:50.437+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-26T20:11:50.437+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-07-26T20:11:50.437+0800 I CONTROL  [initandlisten] 
2017-07-26T20:11:50.437+0800 I CONTROL  [initandlisten] 
2017-07-26T20:11:50.437+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-07-26T20:11:50.500+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/Users/chengyihe/workspace/mongodb/db/diagnostic.data'
2017-07-26T20:11:50.706+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-07-26T20:11:50.706+0800 I INDEX    [initandlisten]          building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-07-26T20:11:50.712+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-07-26T20:11:50.713+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-07-26T20:11:50.713+0800 I NETWORK  [thread1] waiting for connections on port 27017

conclusion
The post shows how to install mongoDB in Mac OS X and the result.


%d bloggers like this: