Mongodb

安装

安装brew

安装mongodb

brew install mongodb

创建目录 ~/data/db

mongod --dbpath=./data/db  

安装可视化管理工具 Robomongo

create->connection->name: myDatabase-> save

到项目目录下

npm install --save mongodb

创建js文件db_server.js

var MongoClient = require('mongodb').MongoClient
    , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myDatabase';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    db.close();
});

node db_server.js

使用

shell

~ mongo

> use myDatabase

> db.pd_info.find({})

…...

db.getCollection('pd_info').aggregate([
    { 
        $match: {
            proname: /玉米/
        }
    },
    { // 联合查找,用集合pd_info数据中的cid去gc_company集合中找,返回的放到cominfo中
        $lookup: {  
            from: 'gc_company',  
            localField: 'cid', 
            foreignField: 'cid', 
            as: 'cominfo' 
        } 
    },
    { 
        $limit: 10 
    },
    { 
        $project: { 
            proname: 1, 
            cid: 1, 
            'cominfo.comname': 1 
        } 
    } 
]);

文档

正则查找数字

If you are wanting to do a pattern match on numbers, the way to do it in mongo is use the $where expression and pass in a pattern match.

> db.test.find({ $where: "/^123.*/.test(this.example)" })
{ "_id" : ObjectId("4bfc3187fec861325f34b132"), "example" : 1234 }
Comments
Write a Comment