Skip to content

Commit 3189eb7

Browse files
author
baruch
authored
Add files via upload
crud
1 parent 485a187 commit 3189eb7

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

Diff for: crud101.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Crud 101
2+
3+
4+
ps -ef |grep mongo |grep -v grep |awk '{print "kill " $2}'
5+
rm -rf /u01/data/mongo01
6+
mkdir --p /u01/data/mongo01
7+
8+
mongod --dbpath /u01/data/mongo01 --logpath /u01/data/mongo01/log.log --logappend --oplogSize 10 --smallfiles --fork
9+
10+
11+
mongoimport --drop -d pcat -c products /u01/demo/products.json
12+
13+
14+
15+
```js
16+
17+
db.products.insertMany([
18+
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true },
19+
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
20+
])
21+
22+
db.products.find({})
23+
24+
db.products.insertOne({
25+
"_id" : "ac9",
26+
"name" : "AC9 Phone",
27+
"brand" : "ACME",
28+
"type" : "phone",
29+
"price" : 333,
30+
"warranty_years" : 0.25,
31+
"available" : true
32+
})
33+
34+
// select * from products where id = "ac9"
35+
db.products.find("_id" : "ac9");
36+
37+
/// lets use varibales
38+
39+
t = db.products
40+
41+
//set myobj to one josn
42+
t = db.products
43+
myobj = t.findOne({_id : ObjectId("507d95d5719dbef170f15c00")})
44+
45+
myobj.limits.voice
46+
47+
48+
//change term_years to 3
49+
myobj.term_years=3
50+
//save
51+
t.save(myobj)
52+
myobj.limits.sms.over_rate=0.01
53+
t.save(myobj)
54+
55+
myobj = t.findOne({_id : ObjectId("507d95d5719dbef170f15c00")})
56+
57+
58+
59+
60+
t.find({"limits.voice":{$exists:true}})
61+
62+
t.find({"limits.voice":{$exists:true}}).count()
63+
64+
// 3
65+
66+
67+
// function
68+
productscode = { }
69+
70+
productscode.b = function() {
71+
var x = db.products.findOne( { _id : ObjectId("507d95d5719dbef170f15c00") } );
72+
if( x == null ) {
73+
print("hmmm, something isn't right? try again");
74+
return 0;
75+
}
76+
return "" + x.limits.voice.over_rate + x.limits.sms.over_rate + x.monthly_price + x.term_years + db.products.find({term_years:3}).count();
77+
}
78+
79+
80+
81+
````
82+

0 commit comments

Comments
 (0)