zoukankan      html  css  js  c++  java
  • Mongodb

    Collection

    A collection is simply a grouping of documents that have the same or a similar purpose.

    A collection is not enforced by a strict schema. Instead, documents in a collection can have a slightly different structure from one another, as needed. 

    Document:

    A document is a representation of a single entity of data in the MongoDB database.

    The records in MongoDB that represent documents are stored as BSON, a lightweight binary form of JSON.

    Write operations are atomic at the document level in MongoDB. 

    If the update causes the document to grow to a size that exceeds the allocated space on disk, MongoDB must relocate that document to a new location on the disk. One way to mitigate document growth is to use normalized objects for properties that can grow frequently.

    mongod:

    This executable starts the MongoDB server and begins listening for database requests on the configured port.

    To stop the MongoDB database from the shell client, use the following commands to switch to the admin database and then shut down the database engine:

    use admin
    db.shutdownServer()

    http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/#clean-shutdown

    If unclean shutdown is detected, delete the data/db/lock file, do mongod --dbpath C:/data/db --repair, then start mongod --dbpath C:/data/db.

    By default httpinterface is off, to view localhost:28017 webpage, you need to start mongod with --httpinterface.

    Shell

    The MongoDB shell is an interactive JavaScript shell that is tightly coupled with the MongoDB data structure.

    JavaScript:

    JavaScript is a typeless language. You do not need to specify in the script what data type the variable is—the interpreter automatically figures out the correct data type for the variable. 

    Define variables: var myString='yes';

    Types: String, Number, Array, Object, Null

    With a .js file, after you launch mongo.exe, you could call load("*.js").

    Access Control db:

    The admindatabase is a special database that provides additional functionality above normal databases. 最开始admin db里没有任何user, 你可以创建user with role userAdminAnyDatabase,这个admin user可以创建专门用来管理数据库的database adminitration accounts,即是users with readWriteAnyDatabase,dbAdminAnyDatabase, and clusterAdmin rights. 

    http://techbus.safaribooksonline.com/book/databases/mongodb/9780133844429/part-i-getting-started-with-nosql-and-mongodb/ch01_html#X2ludGVybmFsX0h0bWxWaWV3P3htbGlkPTk3ODAxMzM4NDQ0MjklMkZjaDA0bGV2MXNlYzJfaHRtbCZxdWVyeT0=

    Manage db:

    Mongo shell vs. Javascript:

    use newDB                                                      myConn=new Mongo("localHost");    newDB = myConn.getDB("newDB");

    newDB.createCollection("newColl")                                                                         newDB.createCollection("myColl");

    db.newColl.insert(doc)

    db.newColl.update(query, update, upsert, multi)

    Mongodb via Java:

    try {
           MongoClient mongoClient = new MongoClient("localhost", 27017);
           mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
           DB db = mongoClient.getDB("words");
           DBCollection collection = db.getCollection("word_stats");
           System.out.println("Number of Documents: " +
             new Long(collection.count()).toString());
         } catch (Exception e) {
           System.out.println(e);
         }

    Mongodb shell: find({"first":"a", "size": {"$gt": 6}}); 

    Java: BasicDBObject query = new BasicDBObject("first", "a");

    query.append("size", new BasicDBObject("$gt", 6)); myColl.find(query);

    To insert a document, you need to first create a BasicDBObject object that represents the document you want to store:

    BasicDBObject doc1 = new BasicDBObject("name", "Fred");
    WriteResult result = myColl.insert(doc1);

    Remove collection:

    DBCollection collection = myDB.getCollection('word_stats');
    BasicDBObject query = new BasicDBObject("first", "a");
    collection.remove(query);

     

  • 相关阅读:
    Window下安装Python
    使用Docker构建一个简单的nginx网页镜像
    solr通过界面管理删除索引和重建索引
    solr8.6添加中文分词器
    php使用solr基础代码类
    window下载安装solr及测试
    Eclipse Android 手机开发作业---心随指动
    Eclipse Android 手机开发作业---空中的气球
    python OpenCV 实现图片的医学处理
    python OpenCV 宽度测量
  • 原文地址:https://www.cnblogs.com/chayu3/p/4244778.html
Copyright © 2011-2022 走看看