User Guide
Inserting MongoDB Documents
This example explains how to insert the same data from the Query Documents - MongoDB page into the inventory collection in the test database. The MongoDB page contains the following MongoDB query:
await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
}
]);
To insert the same dummy data into the inventory collection in the test database using the MongoDB app in Logpresso, run the following query:
json "{}"
| eval items = array(
dict("item", "journal", "qty", 25, "size", dict("h", 14, "w", 21, "uom", "cm"), "status", "A"),
dict("item", "notebook", "qty", 50, "size", dict("h", 8.5, "w", 11, "uom", "in"), "status", "A"),
dict("item", "paper", "qty", 100, "size", dict("h", 8.5, "w", 11, "uom", "in"), "status", "D"),
dict("item", "planner", "qty", 75, "size", dict("h", 22.85, "w", 30, "uom", "cm"), "status", "D"),
dict("item", "postcard", "qty", 45, "size", dict("h", 10, "w", 15.25, "uom", "cm"), "status", "A")
)
| explode items
| parsemap field=items
| mongodb-insert-batch profile="mongo" database="test" collection="inventory"
- The array() function creates a single list object from all objects passed as arguments.
- The dict() function takes consecutive key-value pairs as input and creates a map object.
- The explode command splits records into multiple rows when the specified field value is an array.
- The parsemap command converts each key-value pair into individual fields when the specified field value is a map.
- The mongodb-insert-batch command inserts all values passed as input records into the specified MongoDB collection.
Querying MongoDB Documents
To query all documents in the inventory collection, run the following query:
Conditional Search in MongoDB
To query documents where the status value is D, run the following query:
As shown above, the filter option of the mongodb-docs command accepts filter conditions in the same format as MongoDB queries.
Searching Documents with MongoDB Query Operators
To search for all documents where the status value is either A or D, run the following query:
mongodb-docs profile="mongo" database="test" collection="inventory"
filter="{ status: { $in: [ 'A', 'D' ] } }"
Searching Documents with AND Conditions
To search the inventory collection for documents where the status value is A and the qty value is less than 30, run the following query:
mongodb-docs profile="mongo" database="test" collection="inventory"
filter="{ status: 'A', qty: { $lt: 30 } }"
Searching with OR Conditions
To search the inventory collection for documents where the status value is A or the qty value is less than 30, run the following query:
mongodb-docs profile="mongo" database="test" collection="inventory"
filter="{ $or: [{ status: 'A' }, { qty: { $lt: 30 } }] }"
Updating MongoDB Documents
To update the status field to E for documents in the inventory collection where the item value is planner, run the following query:
json "{}"
| eval item="planner", status="E"
| mongodb-update-batch profile="mongo" database="test"
collection="inventory" keys="item" fields="status"
The options used in the mongodb-update-batch command above are described as follows:
profile: The MongoDB connection profile identifier for the update targetdatabase: The MongoDB database name for the update targetcollection: The MongoDB collection name for the update targetkeys: The field name(s) used as search criteria. To search based on multiple key fields, separate them with commas (,).fields: The field name(s) to update. To update multiple fields, separate them with commas (,).
Deleting MongoDB Documents
To delete all documents in the inventory collection where the status value is A, run the following query:
json "{}"
| eval status="A"
| mongodb-delete-batch profile="mongo" database="test"
collection="inventory" keys="status"
To delete only the first document where the status value is A, you can limit it as follows:
json "{}"
| eval status="A"
| mongodb-delete-batch profile="mongo" database="test"
collection="inventory" keys="status" limit=1
To delete all documents in the inventory collection, run the following query: