これは私には非常に奇妙です。配列onlyIdsをデータベースの集計クエリに入れると、結果が得られません。ただし、5行目から出力されるonlyIdsのコンテンツを次のように配置すると、次のようになります。
["52e953942a13df5be22cf792","52e953942a13df5be22cf793","52e953942a13df5be22cf797"...]
その後、それは動作します。しかし、変数を使用する場合はそうではありません。
この関数:
var onlyIds = [];
for (var i = 0; i < users.length; i++) {
onlyIds.push(users[i]._id);
}
console.log("ids: " + JSON.stringify(onlyIds)); <---------- not empty
db.collection('posts', function(err, collection) {
collection.aggregate([
{$match: {user_id: {$in: onlyIds}}}, <------- not working
{$match: {created:{$gte: 0}}},
{$sort:{"created": -1}},
{$skip: req.body.skip},
{$limit: req.body.limit}
],
function(err, posts) {
var errorNo, content, message;
if (err) {
errorNo = resSend.errorDB;
message = JSON.stringify(err);
} else {
errorNo = resSend.errorNo;
content = posts;
message = "";
--> console.log(JSON.stringify(posts));
}
resSend.sendResponse(res, resSend.errorNo, content, message);
});
});
つまり、なぜこれが機能するのでしょうか。
{$match: {user_id: {$in: ["52e953942a13df5be22cf792","52e953942a13df5be22cf793","52e953942a13df5be22cf797"...]}}}
そしてこれはしません:
{$match: {user_id: {$in: onlyIds}}}
そして、機能しない行は、別の機能で完全に機能します。何かアイデアや悟りはありますか?
編集:次のような答えを見つけて使用するように切り替えます:
collection.find({'user_id': {$in: onlyIdsX}}).toArray(function(err, posts)
どちらも動作しません。
回答:
選択した回答が以下に示すように、検索する変数がObjectIdまたは文字列の場合です。他の人にとっては、db内の変数が一致させようとしているものと同じタイプであることを確認してください。私の場合、両方とも文字列であるはずでしたが、「onlyIds」の1つはObjectIdsでした。
次のコードを試して、ループを変更してください。
var ids = ["52e953942a13df5be22cf792","52e953942a13df5be22cf793","52e953942a13df5be22cf797"];
var obj_ids = [];
for (var i = 0; i < users.length; i++) {
obj_ids.push(new ObjectID(users[i]._id.toString()));
var obj_ids.push(users[i]._id); // <== This will not work if your DB has _id : ObjectID("xyz") [i.e. you are not overiding defaults]
}
そしてvar ObjectID = require('mongodb').ObjectID;
、コードに含める必要があります。
使用する必要があります.toArray(function(err,..
(集約フレームワークを使用したため、あなたの場合ではありません)。これは、使用していない場合にも問題を引き起こしますfindOne()
(これに関する詳細については、ここにリンクがあります)
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
ObjectID = require('mongodb').ObjectID;
var BSON = require('mongodb').BSONPure;
var server = new Server('localhost', 27017, {
auto_reconnect: true
});
var MongoClient = require('mongodb').MongoClient
//let id = your _id, smth like '6dg27sh2sdhsdhs72hsdfs2sfs'...
var users = ["52e953942a13df5be22cf792","52cbd028e9f43a090ca0c1af","52e953942a13df5be22cf797"];
var obj_ids = [];
for (var i = 0; i < users.length; i++) {
obj_ids.push(new ObjectID(users[i].toString()));
//obj_ids.push(users[i]._id); // <== This will not work if your DB has _id : ObjectID("xyz") [i.e. you are not overiding defaults]
}
MongoClient.connect('mongodb://127.0.0.1:27017/YourDBName', function(err, db) {
console.log('err' + err);
db.collection('posts', function(error, collection) {
//collection.find({_id:{$in: users}}),function(err, docs) { //This will not work
collection.find({_id:{$in: obj_ids}}).toArray(function(err, docs) {
console.log("Printing docs from Array. count " + docs.length);
docs.forEach(function(doc) {
console.log("Doc from Array ");
console.dir(doc);
});
});
});
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加