§2023-07-24

?? still don't know how to write mongodb:// uri with replication set

/*
 *  .mongoshrc.js
 *  db = connect("mongodb://127.0.0.1:27017/admin?replicaSet=ys20210904");
 *  db = connect("mongodb://x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?authSource=admin")
 *  db = connect("mongodb://siteRootAdmin:b23258585@x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/?replicaSet=odroid01&authSource=admin");
 *  db.auth("siteRootAdmin", "b23258585");
 *  all the above lines failed
 */

db = 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();

let timerId =  setInterval(() => alert(), 60000); // 単位 is milli-second, 1 sec = 1000
$ mongosh 
Current Mongosh Log ID: 64bf35320643c29791c3256a
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.0
Using MongoDB:          7.0.0-rc8
Using Mongosh:          1.10.0

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

┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
(index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
00'hc4Jammy.yushei.com.tw:27017'11'PRIMARY'11'N2Jammy.yushei.com.tw:27017'12'SECONDARY'22'x8664Arch.yushei.com.tw:27017'12'SECONDARY'└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 10:36:36 GMT+0800 (Taipei Standard Time)
odroid01 [primary] test> ┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
(index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
00'hc4Jammy.yushei.com.tw:27017'11'PRIMARY'11'N2Jammy.yushei.com.tw:27017'12'SECONDARY'22'x8664Arch.yushei.com.tw:27017'12'SECONDARY'└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 10:37:36 GMT+0800 (Taipei Standard Time)
┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
(index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
00'hc4Jammy.yushei.com.tw:27017'11'PRIMARY'11'N2Jammy.yushei.com.tw:27017'12'SECONDARY'22'x8664Arch.yushei.com.tw:27017'12'SECONDARY'└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 10:38:36 GMT+0800 (Taipei Standard Time)
┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
(index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
00'hc4Jammy.yushei.com.tw:27017'11'PRIMARY'11'N2Jammy.yushei.com.tw:27017'12'SECONDARY'22'x8664Arch.yushei.com.tw:27017'12'SECONDARY'└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 10:39:36 GMT+0800 (Taipei Standard Time)
┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
(index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
00'hc4Jammy.yushei.com.tw:27017'11'PRIMARY'11'N2Jammy.yushei.com.tw:27017'12'SECONDARY'22'x8664Arch.yushei.com.tw:27017'12'SECONDARY'└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 10:40:36 GMT+0800 (Taipei Standard Time)
┌─────────┬─────┬─────────────────────────────────┬────────┬───────┬─────────────┐
(index) │ _id │              name               │ health │ state │  stateStr   │
├─────────┼─────┼─────────────────────────────────┼────────┼───────┼─────────────┤
00'hc4Jammy.yushei.com.tw:27017'11'PRIMARY'11'N2Jammy.yushei.com.tw:27017'12'SECONDARY'22'x8664Arch.yushei.com.tw:27017'12'SECONDARY'└─────────┴─────┴─────────────────────────────────┴────────┴───────┴─────────────┘
Tue Jul 25 2023 10:41:36 GMT+0800 (Taipei Standard Time)

(To exit, press Ctrl+C again or Ctrl+D or type .exit)
odroid01 [primary] test> 
// connect to relicationSet odroid01
const db = connect("mongodb://siteRootAdmin:b23258585@x8664Arch.yushei.com.tw:27017,hc4Jammy.yushei.com.tw:27017,n2Jammy.yushei.com.tw:27017/YuSheiDBTest?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();

// Connect to the MongoDB server and select your database
// const conn = new Mongo();
// const db = conn.getDB('your_database_name');

// Replace 'your_capped_collection_name' with the name of your capped collection
const collection = db.getCollection('inLaneCapped');

// Watch the capped collection for changes
const changeStream = collection.watch();

// Function to process each change and print specific fields
const processChange = (change) => {
  if (change.operationType === 'insert') {
   // _id: , plateText: 'NEF6953', cameraSource: '002',  inTime: '2023-07-25T17:17:04',
    const { _id, plateText, cameraSource, inTime } = change.fullDocument; // Add the specific fields you want to print
    print(`Change ID: ${_id}`);
    print(`plateText: ${plateText}`);
    print(`camerSource: ${cameraSource}`);
    print(`inTime: ${inTime}`);
  }
};

// Start the change stream and monitor for changes
const monitorChanges = async () => {
  while (true) {
    const change = await changeStream.tryNext();
    if (change) {
      processChange(change);
    } else {
      // Sleep for a short interval before checking again
      sleep(100);
    }
  }
};

monitorChanges().catch(printjson); // Start monitoring changes

以下

// .mongoshrc.js
db = connect("mongodb://127.0.0.1:27017/admin?replicaSet=ys20210904");
db.auth("siteRootAdmin", "b23258585");
let log =  rs.status().members;

// console.table(log);

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);