Closed happypeter closed 6 years ago
users.json
{"_id":"24Fgub3ZCE9W4iJRk","createdAt":{"$date":"2016-10-14T08:29:18.395Z"},"services":{"password":{"bcrypt":"$2a$10$3s8QZCDR7lcl17e.02HWU.PiLqYzBKrpfdGEwFQCT7gi/4EwYNRyG"},"resume":{"loginTokens":[]}},"username":"buyeril","emails":[{"address":"2986743554@qq.com","verified":false}],"paidCourses":[],"team":[]}
{"_id":"25Y3C3ZMKgnz2ug6J","createdAt":{"$date":"2016-05-16T13:15:35.902Z"},"services":{"password":{"bcrypt":"$2a$10$rCqnDsyirkckx/k.3z.aOurdYFaZZc5nII47gP9VZKqwMx.xfSO06"},"resume":{"loginTokens":[]}},"username":"sidg","emails":[{"address":"59700141@qq.com","verified":false}],"paidCourses":["happypeter-js-kingdom"]}
{"_id":"26kWP8S82mJPjXCqE","createdAt":{"$date":"2016-05-23T10:56:02.212Z"},"services":{"password":{"bcrypt":"$2a$10$LQ3t8o8NPNb1eNVHjwgvK.vn.gXa0O9grTwlqua7uY9FxHU7TRbDa"},"resume":{"loginTokens":[]}},"username":"zhuangsirui","emails":[{"address":"zhuangsirui@gmail.com","verified":false}],"paidCourses":["happypeter-js-kingdom","webpack-react-tricks","webpack-react-mole"]}
{"_id":"2AyLd7ExRyWDuniKC","createdAt":{"$date":"2016-05-24T10:13:45.197Z"},"services":{"password":{"bcrypt":"$2a$10$9nXJE21OTD9WkhB2McEoZ.hWZICrZw/dL6HxpXsLwsBpGweCSv3X2"},"resume":{"loginTokens":[]}},"username":"cgpu456","emails":[{"address":"17528063@qq.com","verified":false}],"paidCourses":["meteor-react-bird"]}
需要从原有数据中拿到的是
username,
paidCourses,
admin,
email
这四项。
mongoimport -d newcat -c contracts --file contracts.json
const fs = require('fs')
const removeLastEl = arr => arr.slice(0, -1)
const jsonToArr = json =>
json.map(t => {
return JSON.parse(t)
})
const getJson = path => removeLastEl(fs.readFileSync(path, 'utf-8').split('\n'))
const userArr = jsonToArr(getJson('../users.json'))
const selectEl = arr =>
arr.map(t => {
const { username, paidCourses, admin } = t
return {
username,
paidCourses,
member: admin
}
})
const arrToJson = arr => arr.map(t => JSON.stringify(t)).join('\n')
const userJson = arrToJson(selectEl(userArr))
const outputFile = 'contracts.json'
fs.writeFileSync(outputFile, userJson)
console.log('saved to ' + outputFile)
总体思路是:
把 contracts 中包含的客户权益,在用户登录和注册的时候,连同其他用户信息一起,保存到 jwt token 中
导入一下