- create instance of mongodb with docker, setting user and password with exposing port 27017
docker run -d --name mongo-server \
-e MONGO_INITDB_ROOT_USERNAME=memo \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
-p 27017:27017 \
mongo
- install cli tool
mongosh
brew install mongosh
- connect to mongo
mongosh "mongodb://memo:secret@127.0.0.1:27017/"
show databases
show dbs
use memo
db.getCollectionNames();
db.getCollectionInfos({ name: "memo" });
simple select (all, by id)
db.getCollection('memo').find({});
db.getCollection('memo').find({'_id': 'fdbf3b6008b064101a6bb0edcf58e67a'});
select with projection (expand)
db.getCollection("memo").find(
{
_id: {
$in: [
"fdbf3b6008b064101a6bb0edcf58e67a",
"66e225a7ada99fe8fd9d15f938a94ce3",
],
},
"data.goods.type": "fmcg",
"data.goods.goodId": "6dd6d7a8c1e282f486fe7877486c7f82",
},
{ "data.goods.goodId": 1, "data.goods.price": 1 },
);
count
db.getCollection('memo').find({'_id': 12345678}).itcount()
pipeline with filter(match), unwind (denormalize), project
db.getCollection("memo").aggregate([
{
$match: {
_id: {
$in: [
"fdbf3b6008b064101a6bb0edcf58e67a",
"2c5be738c6102ba9a33b4d4729e24eff",
],
},
},
},
{
$unwind: {
path: "$data.goods",
},
},
{
$match: {
"data.goods.available": false,
},
},
{
$project: {
_id: 1,
"data.goods.goodId": 1,
"data.goods.name": 1,
"data.goods.price": 1,
},
},
]);
simple insert
db.getCollection("memo").insert({ _id: 12345678 });
db["memo"].insert({ _id: 12345678 });
db.memo.insert({ _id: 12345678 });
insert one record db.collections.insertOne(documnent, options)
db.getCollection("memo").insertOne({
_id: "fdbf3b6008b064101a6bb0edcf58e67a",
data: {
goods: [
{
goodId: "6dd6d7a8c1e282f486fe7877486c7f82",
type: "fmcg",
name: "milk",
price: 10,
available: true,
},
{
goodId: "d1c8034bdf0733018fceb39ca251e1f5",
type: "fmcg",
name: "potato",
price: 15,
available: false,
},
{
goodId: "d1c8034bdf0733018fceb39ca251e1f5",
type: "fmcg",
name: "soy milk",
price: 9,
available: false,
},
],
},
});
insert many records db.collection.insertMany(documents, options)
db.getCollection('memo').insertMany([
{
'_id': '2c5be738c6102ba9a33b4d4729e24eff',
'data': {
'goods': [
{
'goodId': '6dd6d7a8c1e282f486fe7877486c7f82',
'type': 'fmcg',
'name': 'milk',
'price': 10,
'available': true
},
{
'goodId': 'd1c8034bdf0733018fceb39ca251e1f5',
'type': 'fmcg',
'name': 'potato',
'price': 15,
'available': false
},
{
'goodId': 'd1c8034bdf0733018fceb39ca251e1f5',
'type': 'fmcg',
'name': 'soy milk',
'price': 9,
'available': false
}
]
}
},
{
'_id': '66e225a7ada99fe8fd9d15f938a94ce3',
'data': {
'goods': [
{
'goodId': '6dd6d7a8c1e282f486fe7877486c7f82',
'type': 'fmcg',
'name': 'milk',
'price': 10,
'available': true
},
{
'goodId': 'd1c8034bdf0733018fceb39ca251e1f5',
'type': 'fmcg',
'name': 'potato',
'price': 15,
'available': false
},
{
'goodId': 'd1c8034bdf0733018fceb39ca251e1f5',
'type': 'fmcg',
'name': 'soy milk',
'price': 9,
'available': false
}
]
}
}
]);
update multiple nested arrays with filter applied
db.getCollection("memo").update(
{
_id: {
$in: [
"fdbf3b6008b064101a6bb0edcf58e67a",
"2c5be738c6102ba9a33b4d4729e24eff",
],
},
"data.goods.goodId": "6dd6d7a8c1e282f486fe7877486c7f82",
},
{
$set: { "data.goods.$.available": true },
},
{ multi: true },
);
simple delete by math
db.getCollection('memo').remove({'_id': 12345678})
get connection string
db.getMongo();