我正在尝试理解mapReduce函数,并根据此question on stackoverflow使用它按与提供的ID数组相同的顺序对响应进行排序。当我尝试通过提供一系列书籍来提取书籍时,一切正常,但使用ID数组它不起作用。我尝试将id转换为ObjectIds,但仍然没有希望。以下是书籍的数据模型。
from random import randint
min = 1
max = 6
roll=input ('care to roll?')
while roll == ('yes' or 'Yes'):
print ("rolling...")
print randint(min,max)
print randint(min,max)
while randint (1):
break
else:
continue
if roll == ('no' or 'No'):
print ("fine then, see if I care")
提取数据的代码是,
const mongoose = require('mongoose');
const BookSchema = mongoose.Schema({
title: {
type: 'string',
required: true
},
author: {
type: 'string'
},
coverUrl: {
type: 'string',
required: true
},
owner: {
type: 'string',
required: true
},
description: {
type: 'string'
}
});
let Book = module.exports = mongoose.model('Book', BookSchema);
错误!
// booksIds is an array with only two ids which are strings.
// three arguments passed to callback (err, docs, stats)
Book.getBooksByIdsWithOrder = function (booksIds, callback) {
// let book_titles = ["The History of Street Art", "A Night In The Woods"];
booksIds[0] = mongoose.Types.ObjectId(booksIds[0]);
booksIds[1] = mongoose.Types.ObjectId(booksIds[1]);
let ask = {}
ask.map = function () {
var order = inputs.indexOf(this._id);
emit( order, { doc: this } );
};
ask.out = {"inline": 1};
ask.query = { "_id": {"$in": booksIds}};
ask.scope = {"inputs": booksIds};
ask.finalize = function (key, value) {
return value.doc;
}
Book.mapReduce(ask, callback);
};