I created this code to create a TPC-H schema in MongoDB, with some validations:
db.createCollection("lineItem", {
validator: {
$or: [
{
"lineNumber": { $type: "int"}
},
{
"quantity": { $type: "decimal"}
},
{
"extendedPrice": { $type: "decimal"}
},
{
"discount": { $type: "decimal"}
},
{
"tax": { $type: "decimal"}
},
{
"returnFlag": { $type: "string"}
},
{
"lineStatus": { $type: "string"}
},
{
"shipDate": { $type: "string"}
},
{
"commitDate": { $type: "string"}
},
{
"receiptDate": { $type: "string"}
},
{
"shipInstruct": { $type: "string"}
},
{
"shipMode": { $type: "string"}
},
{
"comment": { $type: "string"}
},
{
"order.orderKey": { $type: "int", $exists: true}
},
{
"order.orderStatus": { $type: "string"}
},
{
"order.totalPrice": { $type: "decimal"}
},
{
"order.orderDate": { $type: "string"}
},
{
"order.orderPriority": { $type: "string"}
},
{
"order.clerk": { $type: "string"}
},
{
"order.shipPriority": { $type: "int"}
},
{
"order.comment": { $type: "string"}
},
{
"order.customer.custKey": { $type: "int", $exists: true}
},
{
"order.customer.name": { $type: "string"}
},
{
"order.customer.address": { $type: "string"}
},
{
"order.customer.phone": { $type: "string"}
},
{
"order.customer.acctBal": { $type: "decimal"}
},
{
"order.customer.mktSegment": { $type: "string"}
},
{
"order.customer.comment": { $type: "string"}
},
{
"order.customer.nation.nationKey": { $type: "int", $exists: true}
},
{
"order.customer.nation.name": { $type: "string"}
},
{
"order.customer.nation.comment": { $type: "string"}
},
{
"order.customer.nation.region.regionKey": { $type: "int", $exists: true}
},
{
"order.customer.nation.region.regionName": { $type: "string"}
},
{
"order.customer.nation.region.comment": { $type: "string"}
},
{
"partSupp.availQty": { $type: "int"}
},
{
"partSupp.supplyCost": { $type: "decimal"}
},
{
"partSupp.comment": { $type: "string"}
},
{
"partSupp.part.partKey": { $type: "int", $exists: true}
},
{
"partSupp.part.name": { $type: "string"}
},
{
"partSupp.part.mfgr": { $type: "string"}
},
{
"partSupp.part.brand": { $type: "string"}
},
{
"partSupp.part.type": { $type: "string"}
},
{
"partSupp.part.size": { $type: "int"}
},
{
"partSupp.part.container": { $type: "string"}
},
{
"partSupp.part.retailPrice": { $type: "decimal"}
},
{
"partSupp.part.comment": { $type: "string"}
},
{
"partSupp.supplier.suppKey": { $type: "int", $exists: true}
},
{
"partSupp.supplier.name": { $type: "string"}
},
{
"partSupp.supplier.address": { $type: "string"}
},
{
"partSupp.supplier.phone": { $type: "string"}
},
{
"partSupp.supplier.acctBal": { $type: "decimal"}
},
{
"partSupp.supplier.comment": { $type: "string"}
},
{
"partSupp.supplier.nation.nationKey": { $type: "int", $exists: true}
},
{
"partSupp.supplier.nation.name": { $type: "string"}
},
{
"partSupp.supplier.nation.comment": { $type: "string"}
},
{
"partSupp.supplier.nation.region.regionKey": { $type: "int", $exists: true}
},
{
"partSupp.supplier.nation.region.regionName": { $type: "string"}
},
{
"partSupp.supplier.nation.region.comment": { $type: "string"}
}
]
}
})
It works well (I think), however, I am having some troubles to create a insert code for this collection above. Here is what I'm trying to insert:
db.lineItem.insertOne({
"lineNumber": 1,
"quantity": 17,
"extendedPrice": 24710.35,
"discount": 0.04,
"tax": 0.02,
"returnFlag": "N",
"lineStatus": "O",
"shipDate": "1996-03-13",
"commitDate": "1996-02-12",
"receiptDate": "1996-03-22",
"shipInstruct": "DELIVER IN PERSON",
"shipMode": "TRUCK",
"comment": "egular courts above the",
"order": {
"orderKey": 1,
"orderStatus": "O",
"totalPrice": 172799.49,
"orderDate": "1996-01-02",
"orderPriority": "5-LOW",
"clerk": "Clerk#000000951",
"shipPriority": 0,
"comment": "nstructions sleep furiously among ",
"customer": {
"custKey": 370,
"name": "Customer#000000370",
"address": "oyAPndV IN",
"phone": "22-524-280-8721",
"acctBal": 8982.79,
"mktSegment": "FURNITURE",
"comment": "ges. final packages haggle quickly. slyly bold ",
"nation": {
"nationKey": 12,
"name": "JAPAN",
"comment": "ously. final",
"region": {
"regionKey": 2,
"regionName": "ASIA",
"comment": "ges. thinly even pinto beans ca"
}
}
}
},
"partSupp": {
"availQty": 7030,
"supplyCost": 802.33,
"comment": "p about the enticingly quiet pinto beans. furiously ironic packages are care",
"part": {
"partKey": 1552,
"name": "plum chartreuse sky pale firebrick",
"mfgr": "Manufacturer#4",
"brand": "Brand#41",
"type": "SMALL POLISHED TIN",
"size": 10,
"container": "WRAP CASE",
"retailPrice": 1453.55,
"comment": "onic deposits"
},
"supplier": {
"suppKey": 93,
"name": "Supplier#000000093",
"address": "wd1djjKXT4zBm",
"phone": "26-528-528-1157",
"acctBal": 368.76,
"comment": "yly final accounts could are carefully. fluffily ironic instruct",
"nation": {
"nationKey": 16,
"name": "MOZAMBIQUE",
"comment": "s. ironic",
"region": {
"regionKey": 0,
"regionName": "AFRICA",
"comment": "lar deposits. blithely final packages cajole. regular waters are final requests. regular accounts are according to"
}
}
}
}
});
However, before I even try to run it, MongoDB console show supplier with this code:
... "supplier": {
... Display all 173 possibilities? (y or n)
When I insert and run a find() in this collection, supplier dont have suppKey and it shows me a ane instead of name inside it
I already saw this question that asks about the same problem, however, mine dont have these spaces mentioned there.
What I should do to stop receiving this error?
'> db.staffList.insert(... (code removed)
Display all 175 possibilities? (y or n)
Try copying the code into MS Word and press the button to switch on the hidden characters.
I was getting the same problem after creating the code in Excel. The code was littered with tabs. Remove the tabs and it should stand a better chance of working.
(To get rid of the tab character in Word, replace ^t with nothing.)
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With