aggregate
db.collection.aggregate([
{
"$match": {
"members.member_id": "001"
}
},
{
"$unwind": "$members"
},
{
"$match": {
"members.member_id": "001"
}
},
{
"$replaceWith": "$members"
}
])
mongoplayground
update
db.collection.update({
"members.member_id": "001"
},
{
"$set": {
"members.$.name": "John"
}
},
{
"multi": false,
"upsert": false
})
mongoplayground
python(fastapi) Edit this by yourself
from fastapi import FastAPI, Request, status, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pymongo import MongoClient
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel
CONNECTION_STRING = "mongodb://localhost"
try:
client = MongoClient(CONNECTION_STRING, serverSelectionTimeoutMS=3000)
print('Connected ! Mongo version is', client.server_info()['version'])
except:
print('Disconnected !')
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
)
templates = Jinja2Templates(directory="static")
@app.get("/")
async def root():
db = get_database()
c = db['key'].find({"_id":2})
if (c):
return {"message": c[0]}
return {"message": c}
@app.get("/get/{id}")
async def root(id: int):
db = get_database()
c = list(db['key'].find({"_id":id}))
return {"message": c}
def get_database():
return client['test']
# This is added so that many files can reuse the function get_database()
if __name__ == "__main__":
print('__main__ !')
# Get the database
dbname = get_database()