Skip to content

Instantly share code, notes, and snippets.

@buroz
Created June 29, 2019 13:26
Show Gist options
  • Save buroz/4a8ba6f1e830da302c536cdffa52324a to your computer and use it in GitHub Desktop.
Save buroz/4a8ba6f1e830da302c536cdffa52324a to your computer and use it in GitHub Desktop.
const { MongoClient } = require('mongodb');
(async (url) => {
const connection = await MongoClient.connect(url, {
poolSize: 10,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
useNewUrlParser: true
})
let dbo = connection.db("mydb");
/*
dbo.collection("orders").insertMany([
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
])
dbo.collection("inventory").insertMany([
{ "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
{ "_id" : 5, "sku": null, description: "Incomplete" },
])
*/
dbo.collection('orders')
.aggregate([{ $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }])
.toArray((err, docs) => console.log(JSON.stringify(docs)))
connection.close()
})("mongodb://127.0.0.1:27017");
/*
// ROLE INDEXI
dbo.collection("roles").createIndex(
{ status : 1 },
{
unique: true,
partialFilterExpression: { status: true }
}
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment