実現したいこと
ソート機能を実装したい
borrowedBookNumber でユーザーをソートしたい
参考にした記事
http://qiita.com/n0bisuke/items/6db3ca0bc4fb7fcbd442
https://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html
スキーマは以下
models/user.js
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ slack: { id: { type: String, required: true, }, name: { type: String, required: true, }, profileImageUrl: String, }, borrowedBookNumber: { type: Number, default: 0, }, }); module.exports = mongoose.model('User', userSchema);
route/user.js の中身
node.js
1var express = require('express'); 2var router = express.Router(); 3var User = require('../models/user.model'); 4var log4js = require('log4js'); 5var logger = log4js.getLogger(); 6 7/* GET users listing. */ 8// ユーザー一覧取得 9router.get('/', (req, res, next) => { 10 const options = { 11 sort: { borrowedBookNumber: 1 }, 12 }; 13 User.find({}, options) 14 .then((books) => { 15 res.json(books); 16 }) 17 .catch((e) => { next(e); }); 18}); 19 20module.exports = router;
GET結果
<!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" href="/stylesheets/style.css"> </head> <body> <h1>Unsupported projection option: sort: { borrowedBookNumber: 1 }</h1> <h2></h2> <pre>MongoError: Unsupported projection option: sort: { borrowedBookNumber: 1 } at Function.MongoError.create (/Users/goods/works/bookshelf/node_modules/mongodb-core/lib/error.js:31:11) at queryCallback (/Users/goods/works/bookshelf/node_modules/mongodb-core/lib/cursor.js:212:36) at /Users/goods/works/bookshelf/node_modules/mongodb-core/lib/connection/pool.js:469:18 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)</pre> </body> </html>
const options = {
sort: 'borrowedBookNumber',
};
に変更しても
[ {}, { "_id": "596dce7d8a688ec1687da0b3" }, { "_id": "596dd1911169096c443cef8e" }, { "_id": "596dd4919e7de26ed6314b76" }, { "_id": "596ded379e7de26ed6314b77" }, { "_id": "596ded5b9e7de26ed6314b78" }, { "_id": "597452ca5ea4f6d7170b5d43" } ]
と
_id のみが出てくる
肝心のborrowedBookNumberが出て来ない
どうすれば良いのでしょう?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。