§2023-07-24

Use the new relicationSet

alexlai@hc4Jammy:~$ mongosh
Current Mongosh Log ID: 64bf7cfe68f504823106a327
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
Using MongoDB:          7.0.0-rc8
Using Mongosh:          1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
│ (index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
│    0    │  0  │ 'hc4Jammy.yushei.com.tw:27017'  │   1    │   1   │  'PRIMARY'  │
│    1    │  1  │  'N2Jammy.yushei.com.tw:27017'  │   1    │   2   │ 'SECONDARY' │
│    2    │  2  │ 'x8664Arch.yushei.com.tw:27017' │   1    │   2   │ 'SECONDARY' │
└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 15:42:57 GMT+0800 (Taipei Standard Time)
odroid01 [primary] test> use Learn
switched to db Learn
odroid01 [primary] Learn> 
db.createUser(
  {
    user: "alexlai",
    pwd: "alex1765",
    roles: [ { role: "readWrite", db: "unicorns" }]
  }
);

{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1690271374, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("82a1de8e2a62255d1cb702ee282a14a91593d259", "hex"), 0),
      keyId: Long("7259357468910682118")
    }
  },
  operationTime: Timestamp({ t: 1690271374, i: 1 })
}
odroid01 [primary] Learn> odroid01 [primary] Learn> show tables;

odroid01 [primary] Learn> show tables

odroid01 [primary] Learn> 

db.unicorns.insertOne({name: 'Aurora',gender: 'f', weight: 450});

DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("64bf7f8968f504823106a328") }
}

odroid01 [primary] Learn> show tables;
unicorns
odroid01 [primary] Learn> 

db.unicorns.insertOne({name: 'Leto', gender: 'm', home: 'Arrakeen', worm: false});

{
  acknowledged: true,
  insertedId: ObjectId("64bf80b368f504823106a329")
}

odroid01 [primary] Learn> 

db.unicorns.insertMany( 
    [
        {name: 'Horny', dob: new Date(1992,2,13,7,47),loves: ['carrot','papaya'],weight: 600,gender: 'm',vampires: 63},
        {name: 'Aurora', dob: new Date(1991, 0, 24, 13, 0), loves: ['carrot', 'grape'], weight: 450, gender: 'f', vampires: 43},
        {name: 'Unicrom', dob: new Date(1973, 1, 9, 22, 10), loves: ['energon', 'redbull'], weight: 984, gender: 'm', vampires: 182},
        {name: 'Roooooodles', dob: new Date(1979, 7, 18, 18, 44), loves: ['apple'], weight: 575, gender: 'm', vampires: 99},
        {name: 'Solnara', dob: new Date(1985, 6, 4, 2, 1), loves:['apple', 'carrot', 'chocolate'], weight:550, gender:'f', vampires:80},
        {name:'Ayna', dob: new Date(1998, 2, 7, 8, 30), loves: ['strawberry', 'lemon'], weight: 733, gender: 'f', vampires: 40},
        {name:'Kenny', dob: new Date(1997, 6, 1, 10, 42), loves: ['grape', 'lemon'], weight: 690, gender: 'm', vampires: 39},
        {name: 'Raleigh', dob: new Date(2005, 4, 3, 0, 57), loves: ['apple', 'sugar'], weight: 421, gender: 'm', vampires: 2},
        {name: 'Leia', dob: new Date(2001, 9, 8, 14, 53), loves: ['apple', 'watermelon'], weight: 601, gender: 'f', vampires: 33},
        {name: 'Pilot', dob: new Date(1997, 2, 1, 5, 3), loves: ['apple', 'watermelon'], weight: 650, gender: 'm', vampires: 54},
        {name: 'Nimue', dob: new Date(1999, 11, 20, 16, 15), loves: ['grape', 'carrot'], weight: 540, gender: 'f'},
        {name: 'Dunx', dob: new Date(1976, 6, 18, 18, 18), loves: ['grape', 'watermelon'], weight: 704, gender: 'm', vampires: 165}
    ]
);

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("64bf862668f504823106a336"),
    '1': ObjectId("64bf862668f504823106a337"),
    '2': ObjectId("64bf862668f504823106a338"),
    '3': ObjectId("64bf862668f504823106a339"),
    '4': ObjectId("64bf862668f504823106a33a"),
    '5': ObjectId("64bf862668f504823106a33b"),
    '6': ObjectId("64bf862668f504823106a33c"),
    '7': ObjectId("64bf862668f504823106a33d"),
    '8': ObjectId("64bf862668f504823106a33e"),
    '9': ObjectId("64bf862668f504823106a33f"),
    '10': ObjectId("64bf862668f504823106a340"),
    '11': ObjectId("64bf862668f504823106a341")
  }
}droid01 [primary] Learn> odroid01 [primary] Learn> db.unicorns.countDocuments();
14
odroid01 [primary] Learn> db.unicorns.find();
[
  {
    _id: ObjectId("64bf7f8968f504823106a328"),
    name: 'Aurora',
    gender: 'f',
    weight: 450
  },
  {
    _id: ObjectId("64bf80b368f504823106a329"),
    name: 'Leto',
    gender: 'm',
    home: 'Arrakeen',
    worm: false
  },
  {
    _id: ObjectId("64bf862668f504823106a336"),
    name: 'Horny',
    dob: ISODate("1992-03-12T23:47:00.000Z"),
    loves: [ 'carrot', 'papaya' ],
    weight: 600,
    gender: 'm',
    vampires: 63
  },
  {
    _id: ObjectId("64bf862668f504823106a337"),
    name: 'Aurora',
    dob: ISODate("1991-01-24T05:00:00.000Z"),
    loves: [ 'carrot', 'grape' ],
    weight: 450,
    gender: 'f',
    vampires: 43
  },
  {
    _id: ObjectId("64bf862668f504823106a338"),
    name: 'Unicrom',
    dob: ISODate("1973-02-09T14:10:00.000Z"),
    loves: [ 'energon', 'redbull' ],
    weight: 984,
    gender: 'm',
    vampires: 182
  },
  {
    _id: ObjectId("64bf862668f504823106a339"),
    name: 'Roooooodles',
    dob: ISODate("1979-08-18T09:44:00.000Z"),
    loves: [ 'apple' ],
    weight: 575,
    gender: 'm',
    vampires: 99
  },
  {
    _id: ObjectId("64bf862668f504823106a33a"),
    name: 'Solnara',
    dob: ISODate("1985-07-03T18:01:00.000Z"),
    loves: [ 'apple', 'carrot', 'chocolate' ],
    weight: 550,
    gender: 'f',
    vampires: 80
  },
  {
    _id: ObjectId("64bf862668f504823106a33b"),
    name: 'Ayna',
    dob: ISODate("1998-03-07T00:30:00.000Z"),
    loves: [ 'strawberry', 'lemon' ],
    weight: 733,
    gender: 'f',
    vampires: 40
  },
  {
    _id: ObjectId("64bf862668f504823106a33c"),
    name: 'Kenny',
    dob: ISODate("1997-07-01T02:42:00.000Z"),
    loves: [ 'grape', 'lemon' ],
    weight: 690,
    gender: 'm',
    vampires: 39
  },
  {
    _id: ObjectId("64bf862668f504823106a33d"),
    name: 'Raleigh',
    dob: ISODate("2005-05-02T16:57:00.000Z"),
    loves: [ 'apple', 'sugar' ],
    weight: 421,
    gender: 'm',
    vampires: 2
  },
  {
    _id: ObjectId("64bf862668f504823106a33e"),
    name: 'Leia',
    dob: ISODate("2001-10-08T06:53:00.000Z"),
    loves: [ 'apple', 'watermelon' ],
    weight: 601,
    gender: 'f',
    vampires: 33
  },
  {
    _id: ObjectId("64bf862668f504823106a33f"),
    name: 'Pilot',
    dob: ISODate("1997-02-28T21:03:00.000Z"),
    loves: [ 'apple', 'watermelon' ],
    weight: 650,
    gender: 'm',
    vampires: 54
  },
  {
    _id: ObjectId("64bf862668f504823106a340"),
    name: 'Nimue',
    dob: ISODate("1999-12-20T08:15:00.000Z"),
    loves: [ 'grape', 'carrot' ],
    weight: 540,
    gender: 'f'
  },
  {
    _id: ObjectId("64bf862668f504823106a341"),
    name: 'Dunx',
    dob: ISODate("1976-07-18T10:18:00.000Z"),
    loves: [ 'grape', 'watermelon' ],
    weight: 704,
    gender: 'm',
    vampires: 165
  }
]
Learn> db.unicorns.find().count();
14


~/.mongoshrc.js is as

b = connect("mongodb://siteRootAdmin:b23258585@x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?replicaSet=odroid01&authSource=admin");

// let log =  rs.status().members;
// console.table(log);


function alert() {
        let log =  rs.status().members;
        let simpleLog = [];
        // console.log(log);
        for (let i = 0; i < log.length; i++){
            // console.log("_id: ", log[i]._id, "name: ", log[i].name, "health: ", log[i].health, "state: ", log[i].state, "stateStr: ", log[i].stateStr);
                simpleLog[i] = {
                        "_id": log[i]._id,
                        "name": log[i].name,
                        "health": log[i].health,
                        "state": log[i].state,
                        "stateStr": log[i].stateStr
                }
        }
        console.table(simpleLog);
        console.log(Date().toLocaleString()); 
}

alert();