install link _ https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
Refers to your current database.
db
To switch to the dbname database
use dbname
Insert new document into the collection
db.collectionName.insertMany([
{ name: "suho", age: 20, size: { h: 188, w: 60}, color: ["black", "white"] },
{ name: "ect", age: 10, size: { h: 138, w: 30}, color: ["green", "red"] }
])
To return all documents in collectionName collection
db.collectionName.find({})
To format the results
db.collectionName.find({}).pretty()
To return documents where name equals "suho"
db.collectionName.find( { name: "suho" } );
db.collectionName.find( { size: { h: 188, w: 30 } } )
db.collectionName.find( { color: [ "red", "green" ] } )
To return the _id, name and the age fields from all documents in the collectionName
db.collectionName.find( { }, { name: 1, age: 1 } );
db.collectionName.find( {}, { _id: 0, name: 1, age: 1 } );
----
CRUD ref _ https://docs.mongodb.com/manual/crud/
생년월일로 나이를 필터링하는 로직을 짰다.
case 1:
정확한 값을 넣어 필터링
@app.route('/api/age/1/<filter>/<filter_text>')
def get_data_filter(filter, filter_text):
if (filter == "dateOfBirth"):
myquery = { "$where": "/.*" +filter_text+ ".*/.test(this." +filter+ ")" }
else:
myquery = { filter : { "$regex": ".*" +filter_text+ ".*" } }
data = mycol.find( myquery )
# data = mycol.find( { filter:int(filter_text)} )
return render_template('index.html', data = data)
case 2:
1930-2000 포맷 형식으로 필터링
@app.route('/api/age/2/<start>/<end>')
def get_age_filter(start, end):
# myquery = { "dateOfBirth" : { $lt } }
start = start + "0000"
end = end + "9999"
myquery = { "dateOfBirth" : { "$gte": int(start), "$lte": int(end) } }
data = mycol.find( myquery )
return render_template('index.html', data = data)
You can Use $options => i for case insensitive search. Giving some possible examples required for string match.
@app.route('/api/hearing/main/<filter>/<filter_text>')
def get_data_filter(filter, filter_text):
if (filter == "dateOfBirth" or filter == "date" or filter == "gender" or filter == "tonal" or filter == "experience"):
myquery = { "$where": "/.*" +filter_text+ ".*/.test(this." +filter+ ")" }
else:
myquery = { filter : { "$regex": ".*" + filter_text + ".*", "$options": "i" } }
data = mycol.find( myquery )
# data = mycol.find( { filter:int(filter_text)} )
return render_template('index.html', data = data)
regex포맷 에서의 insensitive search방법
$regex
Provides regular expression capabilities for pattern matching strings in queries
---------------------
서버에 몽고디비 다운받고 실행하기
우분투 18.04 aws버전 사용중 4.4가 잘 다운이 되는것같지가 않아 3버전을 다운받게되었다. 작동은 잘되었다.
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04
어드민계정 설정, 패스워드 설정을 하지 않으면 해킹봇이 있는지 다음날 보면 디비가 전부 털려있고 그런다. 아래 도큐먼트를 보고 꼭 설정을 하자
https://docs.mongodb.com/manual/tutorial/enable-authentication/
uri = 'mongodb://adminuser:password@ipaddress:27017/?authSource=admin'