All Posts By

Kunal Gautam

MongoDB | CRUD Operations

By | MongoDB Learning

1) Creating Record:

Record can be inserted using:

db.tableName.insert()
db.tableName.insertOne()
db.tableName.insertMany()
  • .insert() can accept single or multiple records. Returns WriteResult/BulkWriteResult.

Example showing inserting one record

db.tableName.insert(
{
name: "John",
age: 23,
skills: {
    programming: "PHP, Java, Python",
    database: "MongoDB, SQLITE"
}
}
)

Example showing Multiple Records

db.inventory.insert([
   { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
   { item: "paper", qty: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
   { item: "planner", qty: 0, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
])

  • .insertOne() accept one record at a time. Returns record id inserted.
db.tableName.insertOne(
{
name: "John",
age: 23,
skills: {
    programming: "PHP, Java, Python",
    database: "MongoDB, SQLITE"
}
}
)

  • .insetMany() accept multiple record at a time. Return record ids inserted.
db.inventory.insertMany([
   { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
   { item: "paper", qty: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
   { item: "planner", qty: 0, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
])

2. Reading Record

  • To see all tables (It is called as collections)
show collections
  • To read all records
db.tableName.find()

or

db.tableName.find({})
  • To return data in readable format
    db.tableName.find().pretty()
  • To find a record with name = John inside tableName table:
db.tableName.find({name: "John"})
  • To find all record returning name key:
db.tableName.find({}, {name:1})

or to exclude _id

db.tableName.find({}, {name:1, _id:0})

3. Modifying/Update the Record

*Note: If the field to be update doesn't exsists, it will be added, else it will be updated*

To update all record in the table

db.tableName.updateMany({}, {$set: {status: "Active"}})

To update all record matching query

Update data from table where name = John

db.tableName.updateMany({name: "John"}, {$set: {status: "Inactive"}})

To update first matching query record

db.tableName.updateOne({name: "John"}, {$set: {status: "Active"}})

Overwrite/Replace record
update record with name = john by deleting exsisting keys and values and add newKey with Value

db.tableName.update({name: "John"}, {newKey: "Value"})

Replace Record

db.tableName.replaceOne({_id: ObjectId("sh9oiulukskji")}, {newKey: "Value"})

4. Deleting Record

To delete all record matching query

Delete data from table where name = John

db.tableName.deleteMany({name: "John"})

To delete one First matching query record

db.tableName.deleteOne({name: "John"})

Delete all records

db.tableName.remove()

Delete Table/Collections

db.tableName.drop()


Notes:

  • .find() won't return all records if record set is large. For example out of 100 Record it will return 10 and wait for input for fetching next 10 records. It is like pagination. to view all data we can use .forEach() function. e.g.

db.tableName.find({}).forEach((data)=>{printjson(data)})

MongoDB | Getting Started

By | MongoDB Learning

All commands mentioned need to be run on mongo shell

1) To see list of database

show dbs

It will show list of Databases along with the size. If a databse is empty, it won't be shown by the command.

2) To use a database

use databaseName

It will switch to the databaseName databse (If database won't exists, it will create one)

3) To see which database is in use

db

It will return the database currently in use

4) Seeking Help

db.help()

To show available functions

db.databaseName.help()

To show available functions with database

5) Delete Database

db.dropDatabase()

To drop the database


Note:

  • If familiar with SQL, the tables are called collections and records are called documents in MongoDB

LoRa communication with Satellite on a Budget

By | Electronics Laboratory, Radio Communicaion

Radio and Wireless communication has always amazed me since I was a kid. We can not imagine our life without relying on wireless communication in current date. Be it WiFi or Cellular connectivity, almost everything rely a lot on Wireless Communication. The Wireless Technology has developed a lot since its inception. One of the interesting technology is LoRa, which stands for Long Range.

Although communication with satellite is nothing new. My friend Martin from U.K. (Call Sign G0PYB) is doing it since ages. The LoRa communication using satellite is something new and interesting. The LoRa communication requires much less power and have long range compared to other wireless protocols. With launch of many Cubesats (basically small satellites) in LEO (Low Earth Orbit), for educational and scientific purpose, anyone with access to basic hardware can access to LoRa communication using Satellite.

Interestingly, at the time of writing, there is one project named TinyGS, which will help you to get started with it. As per their website:

"Initially TinyGS was born under the name ESP32 Fossa Groundstation, it was developed as a "weekend" project for the FossaSAT-1 LoRa satellite. We are passionate about space and created this project to be able to track and use the satellites and to learn and experiment about radio. Currently the network is open to any LoRa satellite and we also support other flying objects that have a compatible radio modulation with our hardware such as FSK, GFSK, MSK, GMSK, LoRa and OOK. And the project was renamed to TinyGS."

There is very helpful community on Telegram (https://t.me/joinchat/DmYSElZahiJGwHX6jCzB3Q) where people are very helpful to get started with the TinyGS Project. The recommended hardware are listed at wiki page of TinyGS i.e. at https://github.com/G4lile0/tinyGS/wiki#hardware

This post is all about getting started with TinyGS project with very basic hardware in a budget friendly manner. Please note that connecting to OLED display is optional. To get started you would need the following Bill of Material (BOM):

Read More

Disable WordPress Formatted Entities

By | WordPress

Recently while I was working on my Linux blog, I noticed that WordPress started formatting "--" as "—", which was quite frustrating as commands become useless without proper attributes/switches.

The following is the table with list of text which Replaces common plain text characters with formatted entities.
Information taken from wptexturize()

source texttransformed textsymbol name
"---""—"em-dash
" -- ""—"em-dash
"--""–"en-dash
" - ""–"en-dash
"...""…"ellipsis
``opening quote
"hello“helloopening quote
'hello‘helloopening quote
''closing quote
world."world.”closing quote
world.'world.’closing quote
" (tm)"" ™"trademark symbol
1234"1234″double prime symbol
1234'1234′prime symbol
'99’99apostrophe before abbreviated year
Webster'sWebster’sapostrophe in a word
1234x12341234×1234multiplication symbol

To disable the wptexturize() function, one can add following to the function.php of currently activated theme or  by creating a plugin

add_filter('run_wptexturize', '__return_false');

I've chosen to create a plugin and add the above code, as with changing theme, I don't need to worry about the problem arising again.