Created
August 4, 2017 10:05
-
-
Save wxs77577/c05d3e3a134e04164bf08ffc659a327e to your computer and use it in GitHub Desktop.
Express with Async Mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require('mongodb').MongoClient | |
const express = require('express') | |
const multer = require('multer') | |
const bodyParser = require('body-parser') | |
const upload = multer({ dest: 'uploads/' }) | |
const router = express.Router() | |
const app = express() | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
let db, m | |
const Post = { | |
collection: 'posts', | |
async query(params = []) { | |
for (let k in this.relations) { | |
let v = this.relations[k] | |
let unwind = null | |
if (v.unwind) { | |
unwind = v.unwind | |
delete v.unwind | |
} | |
params.push({ $lookup: v }) | |
if (unwind) { | |
params.push({ $unwind: unwind }) | |
} | |
} | |
console.log(params); | |
return await db.collection(this.collection).aggregate(params).toArray() | |
}, | |
relations: { | |
user: { from: 'users', localField: 'user_id', foreignField: 'id', as: 'user', unwind: '$user' }, | |
properties: { from: 'properties', localField: 'property_ids', foreignField: 'id', as: 'properties' }, | |
} | |
} | |
const main = async () => { | |
db = await MongoClient.connect('mongodb://localhost/yizhi') | |
m = name => db.collection(name) | |
// db.news = db.collection('news') | |
// db.users = db.collection('users') | |
// let ret = await db.collection('posts').aggregate([ | |
// { $lookup: { from: 'users', localField: 'user_id', foreignField: 'id', as: 'user' } }, | |
// { $unwind: '$user' }, | |
// { $unwind: '$property_ids' }, | |
// { $lookup: { from: 'properties', localField: 'property_ids', foreignField: 'id', as: 'properties' } }, | |
// // {$unwind: '$properties'}, | |
// { $limit: 1 } | |
// ]).toArray() | |
// let ret = await Post.query() | |
// console.log(ret); | |
// db.news.update({}, {$rename: {categoyIds: 'categoryIds'}}) | |
// db.collection('news').updateMany({id: {$lt: 5}}, {$set: {categoyIds: [1,2,3]}}) | |
// db.collection('news').updateMany({id: {$gte: 5}}, {$set: {categoyIds: [3,4,5]}}) | |
} | |
main() | |
app.use((req, res, next) => { | |
req.input = (name, defaultValue) => { | |
let value = req.body[name] ? req.body[name] : (req.query[name] ? req.query[name] : defaultValue) | |
return typeof defaultValue === 'number' ? Number(value) : (typeof defaultValue === 'boolean' ? Boolean(value) : value) | |
} | |
next() | |
}) | |
router.get('/news', async (req, res, next) => { | |
try { | |
let filter = { categoryIds: 3 } | |
// console.log(req.input('category', '').split(',').map(v => parseInt(v))); | |
let total = await db.collection('news').find({ | |
categoryIds: { $gt: 1 } | |
}).toArray() | |
return res.json(total) | |
let data = await db.collection('news').aggregate([ | |
{ $match: filter }, | |
{ $skip: req.input('skip', 0) }, | |
{ $limit: req.input('limit', 10) }, | |
{ $sort: { id: -1 } } | |
]).toArray() | |
console.log(data); | |
// res.append('total', total) | |
res.send(data) | |
} catch (e) { | |
res.json(e) | |
} | |
}) | |
app.use('/api', router) | |
app.get('/posts', async (req, res) => { | |
// let ret = await db.collection() | |
let ret = await Post.query([ | |
{$limit: 10} | |
]) | |
res.send(ret) | |
}) | |
app.listen(3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment