Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Export all collection documents in MongoDB to a JSON file:
mongoexport --db YOUR_DATABASE --collection YOUR_COLLECTION_NAME --out YOUR_OUTPUT_FILE_NAME.json

NOTE: do not use this command for full database backup! Use mongodump instead

#mongodb #export #mongoexport #mongodump #collection #database
What is Capped Collections in MongoDB?

Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

Circular buffers are filled and emptied, so capped collection has similar behaviours.

Some notes:
- Automatic Removal of Oldest Documents
- Capped collections have an _id field and an index on the _id field by default.
- You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.
- You cannot shard a capped collection.
- You must create capped collections explicitly

To create a capped collection:

db.createCollection( "cpu_metrics", { capped: true, size: 100000 } )

The name of the collection in our example is cpu_metrics with the flag of capped=true and size of the collection.

You can also specify maximum number of documents for the collection using max as following example:

db.createCollection("cpu_metrics", { capped : true, size : 5242880, max : 5000 } )

NOTE: the size argument is always required, even when you specify max number of documents.

Check whether collection is capped or not:

db.collection.isCapped()


Convert a non-capped collection to capped:

db.runCommand({"convertToCapped": "mycoll", size: 100000});

#mongodb #mongo #capped #collection #capped_collection
MongoDB data types

String: You know what it is!

Integer: This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.

Boolean: True/False

Double: This type is used to store floating point values.

Arrays: [list, of, elements]

Timestamp: This can be handy for recording when a document has been modified or added.

Object: This datatype is used for embedded documents. Like {"images": {"a": "ali", "b": "reza"}}

Null: This type is used to store a Null value.

Date: This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.

Object ID: This datatype is used to store the document’s ID.

There are some more like Code, Regex and so which is used less than other data types.

#mongodb #data_type #mongo #database #collection #object