[MongoDB] Insert, find -- 1

MongoDB is JSON Document:

How to start MongoDB client:

mongod //start the server

mongo // start the cli

How to restore a database:

mongorestore can create a new database or add data to an existing database.

If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.

Read More:http://docs.mongodb.org/manual/reference/program/mongorestore/

Download the resoure: http://poeticoding.com/downloads/mongodb/mongodb_poeticoding_blog.zip

How to import json file into database:

mongoimport --db simple --collection people --jsonArray data.json

Read More: http://docs.mongodb.org/manual/reference/program/mongoimport/

Simple commands:

//show all the database:
show dbs;

//show all collections:
show collections;

//create or use one database
use <database_name>

Insert:

The data you have:

{
    title: "My first blog post",
    author:{
        firstName: "YY",
        lastName: "KK",
        email: "[email protected]"
    },
    tags: ["coding", "database"],
    pubData: new Date
}

Cmd:

//insert into the posts collection
db.posts.insert({
    title: "My first blog post",
    author:{
        firstName: "YY",
        lastName: "KK",
        email: "[email protected]"
    },
    tags: ["coding", "database"],
    pubData: new Date
});

Note: _id should be unique.

Find:

//get the oldest inserted data
db.posts.findOne();
//get all the data
db.posts.find();
db.posts.find().count(); //count the amount
db.posts.find().pretty(); //make output looks pretty
//Find according to the criteria

//Find the title which is AngularJS
db.posts.find({title: "AngularJS"}); 

//Find the title whcih contains AngularJS
db.posts.find({title: /AngularJS/i}); 
//Find title contains ‘ios‘, only show title, not _id in the result
db.posts.find({title: /ios/i}, {title:1, _id: 0});

Javascript code for this: (something should looks like)

    Posts.find({title: /ios/i}).select("title").exec(function(err, data) {
        response.json(200, data);
    })
时间: 2024-10-24 01:44:41

[MongoDB] Insert, find -- 1的相关文章

mongodb insert 错误 ERROR: MessagingPort::call() wrong id got:367 expect:366 解决过程

这个错误网上介绍比较少, 记录一下 最近工作中遇到mongodb insert 时候报错, 现象: Thu Sep 24 09:19:15.011 ERROR: MessagingPort::call() wrong id got:367 expect:366 toSend op: 2004 response msgid:2931523219 response len:  157 response op:  1 remote: xxxxxxxx Thu Sep 24 09:19:15.011  

Java MongoDB insert

public class  Foo implements IdObject { public String id;    public String firstName;    private String lastName;        @Override     String getId() {        return id;    }        @Override    public void toDocument(Document doc) {        doc.appen

【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update

1, Insert MongoDB is database storing document object, the type of which is called Bson.(like JSON); Example:  // document defination Now after using command[db.posts.insert(doc)], you will insert record successfully if seeing the The following pictu

MongoDB insert/update/one2many案例

以博文与评论为例,博文有标题内容,对应多个评论,评论有评论人.评论内容等. (1)插入一条博文: db.blog.insert( {'_id':'11','title':'this is blog title1','content':'this is blog content1'} ) (2)更新一条博文 db.blog.update( {'_id':'11'}, {$set:{'title':'this is blog title2','content':'this is blog conte

【翻译】MongoDB指南/CRUD操作(二)

[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关注),写确认(写关注) 1 更新文档 1.1 更新 MongoDB提供下列方法用于更新一个集合 db.collection.updateOne() 更新使用指定过滤器匹配到的文档,即使过滤器匹配到多个文档,也只会更新一个文档. 3.2版本新增特性. db.collection.updateMany(

java操作mongoDB数据库的简单实例

首先导入mongoDB的jar包 http://pan.baidu.com/s/1bnGMJRD //DataBase.java package com.mongodb.test; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import

SQL to MongoDB Mapping Chart

http://docs.mongodb.org/manual/reference/sql-comparison/ In addition to the charts that follow, you might want to consider the Frequently Asked Questions section for a selection of common questions about MongoDB. Terminology and Concepts The followin

DB Intro - MongoDB Basic

mongoDB basic from:http://www.tutorialspoint.com/mongodb prject:https://github.com/chenxing12/l4mongodb overview getting-start collection dataType insert find Overview MongoDB is a cross-platform, document oriented database that provides, high perfor

MongoDB - MongoDB CRUD Operations

CRUD operations create, read, update, and delete documents. Create Operations Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection. MongoDB provides th