mongodb 杂记

1. insertDocument :: caused by :: 11000 E11000 duplicate key error index:
perf_test.test.$_id_ dup key: { : ObjectId(‘597edae0faa2ec5e6859e7a3’) }
code is wrong, there have two line to insert the same document, so fail

2. 16555: ‘exception: $multiply only supports numeric types, not String
in $multiply, you need use number insead of string

3. 16810: ‘exception: bad query: BadValue unknown top level operator: $avg’
there no $avg field in document, you should remove $, let mongodb know it, to
find it from stage output

4. Group by null
The _id field is mandatory; however, you can specify an _id value of null to
calculate accumulated values for all the input documents as a whole.

5. group with system variable
$$ROOT –> current whole document

6. system variable & let variable
https://docs.mongodb.com/manual/reference/operator/aggregation/let/#exp._S_let
https://docs.mongodb.com/manual/reference/aggregation-variables/#agg-system-
variables

7. type
New in version 3.4.
https://docs.mongodb.com/manual/reference/operator/aggregation/type/#exp._S_type

8. limit
The maximum BSON document size is 16 megabytes.
MongoDB supports no more than 100 levels of nesting for BSON documents.
A single collection can have no more than 64 indexes.
index key limit?

https://docs.mongodb.com/manual/reference/limits/#BSON-Document-Size

9. $group & $sort memory limit
The $group stage has a limit of 100 megabytes of RAM. By default, if the stage
exceeds this limit, $group will produce an error. However, to allow for the
handling of large datasets, set the allowDiskUse option to true to enable
$group operations to write to temporary files.

11. $sort Operator and Performance
$sort operator can take advantage of an index when placed at the beginning of
the pipeline or placed before the $project, $unwind, and $group aggregation
operators. If $project, $unwind, or $group occur prior to the $sort operation,
$sort cannot use any indexes.

12. run command from Java driver
eval command:
{
eval: ,
args: [ , … ],
nolock:
}

map to java ==>
Document command = new Document();
command.put(“eval”, “function () { var result = db.test.findOne( { test_name :
\”test_name0\” }); return Object.bsonsize(result); }”);
command.put(“args”, “”);

commnd list
https://docs.mongodb.com/manual/reference/command/

13. calculate median in mongodb
The simplest way to compute the median would be with these two statements
(assuming the attribute on which we want to compute the median is called a and
we want it over all documents in the collection, coll):

count = db.coll.count();
db.coll.find().sort( {“a”:1} ).skip(count / 2 - 1).limit(1);

https://www.compose.com/articles/mongo-metrics-finding-a-happy-median/

14. how to define the primary key in self
_id field is reserved for primary key in mongodb, and that should be a unique
value. If you don’t set anything to _id it will automatically fill it with
“MongoDB Id Object”. But you can put any unique info into that field.

15. if you put two difference thing as primary key, then group by may not
work well

16. index and compund index
index max limit is 64, compund index max limit is 31.
the 64 means the all index count should not exceed 64(normal and compund)

17. ??
The total size of an index entry, which can include structural overhead
depending on the BSON type, must be less than 1024 bytes.

mongodb shell

  • show command
show dbs
use <db name>
show collections
db.<collectionName>.find()
  • list help for collection
db.<collectionName>.help()
  • create index
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        }
]
> db.test.createIndex({"test_name": 1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        },
        {
                "v" : 1,
                "key" : {
                        "test_name" : 1
                },
                "name" : "test_name_1",
                "ns" : "perf_test.test"
        }
]
  • drop index
> db.test.dropIndex("test_name")
{
        "nIndexesWas" : 2,
        "ok" : 0,
        "errmsg" : "index not found with name [test_name]"
}
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        },
        {
                "v" : 1,
                "key" : {
                        "test_name" : 1
                },
                "name" : "test_name_1",
                "ns" : "perf_test.test"
        }
]
> db.test.dropIndex( {"test_name": 1} )
{ "nIndexesWas" : 2, "ok" : 1 }
> db.test.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "perf_test.test"
        }
]

mongodb simple auth

  • start mongod without –auth parameter, disable auth
  • connect to this instance with mongo, then create a super user
  • restart mongod with –auth
$ mongo --port 27017 -u siteUserAdmin -p password --authenticationDatabase admin 
  • re-connect to it with mongo, then create normal db user
$ mongo
> user admin
> db.auth("user", "password")
> use admin
> db.createUser(
    {
      user: "superuser",
      pwd: "12345678",
      roles: [ "root" ]
    }
  )

> use reporting
> db.createUser(
    {
      user: "reportsUser",
      pwd: "12345678",
      roles: [
         { role: "read", db: "reporting" },
         { role: "read", db: "products" },
         { role: "read", db: "sales" },
         { role: "readWrite", db: "accounts" }
      ]
    }
  )

you need edit config ld.so.conf after you installed C driver for MongoDB

After you installed C driver from source, it print below help message, you
should take care.

Libraries have been installed in:
   /usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

it means that you should append /usr/local/lib to LD_LIBRARY_PATH env,
and let your linker can find mongoc lib

if you have root permision, the easy way is to edit /etc/ld.so.conf ,
after you do that, you should run below command, and make sure everything is
fine

$ ldconfig
$ ldconfig -p | grep mongo
        libmongoc-1.0.so.0 (libc6,x86-64) => /usr/local/lib/libmongoc-1.0.so.0
        libmongoc-1.0.so (libc6,x86-64) => /usr/local/lib/libmongoc-1.0.so

compile/install C++ driver for MongoDB fail

C++ 11 driver

  • install C driver before your install C++ driver
  • download from here and extract it to your directory
  • compile
$ cd mongo-cxx-driver-r3.1.2/build
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
  • fail for need higher version cmake and gcc, gcc in my env is 4.8, this version is same with the gcc version in here . but my cmake version is 2.8
  • if you want to move on, please update your gcc and cmake to higher version

PS:
I got below error when you install some package, it means, you gcc version is
low

# rpm -ivh cmake-3.9.0-2.fc27.x86_64.rpm
error: Failed dependencies:
        cmake-data = 3.9.0-2.fc27 is needed by cmake-3.9.0-2.fc27.x86_64
        cmake-filesystem = 3.9.0-2.fc27 is needed by cmake-3.9.0-2.fc27.x86_64
        libform.so.6()(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libjsoncpp.so.11()(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libncurses.so.6()(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        librhash.so.0()(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libstdc++.so.6(CXXABI_1.3.8)(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libstdc++.so.6(CXXABI_1.3.9)(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libstdc++.so.6(GLIBCXX_3.4.20)(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libstdc++.so.6(GLIBCXX_3.4.21)(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libtinfo.so.6()(64bit) is needed by cmake-3.9.0-2.fc27.x86_64
        libuv.so.1()(64bit) is needed by cmake-3.9.0-2.fc27.x86_64

My question is that: does there have any page can explain which gcc version
map to CXXABI version

how to install/compile MongoDB C driver

  • download source code from here
  • extract it and compile
$ ./configure --help ;list all options
$ ./configure --disable-automatic-init-and-cleanup 
$ make
$ sudo make install ; or su to root, then make install
  • write a sample code and export PKG_CONFIG_PATH for pkg-config, then compile with gcc
$ gcc -o connect MongoDBTest.c $(pkg-config --cflags --libs libmongoc-1.0)

driver compatibility
https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/
example:
http://mongoc.org/libmongoc/current/tutorial.html#starting-mongodb

install MongoDB from tar in Linux

install MongoDB from tar in Linux

  • download tar file from here
  • extract it to any directory, add install/bin PATH with export
  • create data directory
    mkdir -p /data/db

  • start mongod server, when everything is ready, you call use mongo shell to contact with mongodb

LTspice introduction - 16 电阻的伏安特性

Version 4
SHEET 1 880 680
WIRE 112 32 -80 32
WIRE -80 96 -80 32
WIRE 112 112 112 32
WIRE 112 192 112 176
WIRE -80 240 -80 176
WIRE 112 240 112 192
WIRE 112 240 -80 240
WIRE 112 272 112 240
FLAG 112 272 0
SYMBOL voltage -80 80 R0
WINDOW 123 0 0 Left 2
WINDOW 39 0 0 Left 2
SYMATTR InstName V1
SYMATTR Value 5
SYMBOL res 96 96 R0
SYMATTR InstName R1
SYMATTR Value 50
TEXT 144 232 Left 2 !.dc V1 550mV 750mV 10mV

将以上内容保存成2V-I-Curve_Resistor_DC_sweep.asc文件,用LTspice打开,然后执行Run进行仿真,结果如下: