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.