MongoDBの階層構造検索に関する思い違いの修正

This commit is contained in:
otofune 2017-11-06 16:22:18 +09:00
parent b266ed3e4f
commit 64be0d6ded
8 changed files with 15 additions and 36 deletions

View file

@ -14,7 +14,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Calculate drive usage // Calculate drive usage
const usage = ((await DriveFile const usage = ((await DriveFile
.aggregate([ .aggregate([
{ $match: { metadata: { user_id: user._id } } }, { $match: { 'metadata.user_id': user._id } },
{ {
$project: { $project: {
length: true length: true

View file

@ -40,10 +40,8 @@ module.exports = async (params, user, app) => {
_id: -1 _id: -1
}; };
const query = { const query = {
metadata: { 'metadata.user_id': user._id,
user_id: user._id, 'metadata.folder_id': folderId
folder_id: folderId
}
} as any; } as any;
if (sinceId) { if (sinceId) {
sort._id = 1; sort._id = 1;

View file

@ -24,11 +24,9 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Issue query // Issue query
const files = await DriveFile const files = await DriveFile
.find({ .find({
metadata: { 'metadata.name': name,
name: name, 'metadata.user_id': user._id,
user_id: user._id, 'metadata.folder_id': folderId
folder_id: folderId
}
}); });
// Serialize // Serialize

View file

@ -21,9 +21,7 @@ module.exports = async (params, user) => {
const file = await DriveFile const file = await DriveFile
.findOne({ .findOne({
_id: fileId, _id: fileId,
metadata: { 'metadata.user_id': user._id
user_id: user._id
}
}); });
if (file === null) { if (file === null) {

View file

@ -20,19 +20,14 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [fileId, fileIdErr] = $(params.file_id).id().$; const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) return rej('invalid file_id param'); if (fileIdErr) return rej('invalid file_id param');
console.dir(user)
// Fetch file // Fetch file
const file = await DriveFile const file = await DriveFile
.findOne({ .findOne({
_id: fileId, _id: fileId,
metadata: { 'metadata.user_id': user._id
user_id: user._id
}
}); });
console.dir(file)
if (file === null) { if (file === null) {
return rej('file-not-found'); return rej('file-not-found');
} }
@ -42,7 +37,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter // Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$; const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
if (nameErr) return rej('invalid name param'); if (nameErr) return rej('invalid name param');
if (name) updateQuery.name = name; if (name) updateQuery['metadata.name'] = name;
// Get 'folder_id' parameter // Get 'folder_id' parameter
const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$; const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
@ -50,7 +45,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
if (folderId !== undefined) { if (folderId !== undefined) {
if (folderId === null) { if (folderId === null) {
updateQuery.folder_id = null; updateQuery['metadata.folder_id'] = null;
} else { } else {
// Fetch folder // Fetch folder
const folder = await DriveFolder const folder = await DriveFolder
@ -63,21 +58,17 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('folder-not-found'); return rej('folder-not-found');
} }
updateQuery.folder_id = folder._id; updateQuery['metadata.folder_id'] = folder._id;
} }
} }
const updated = await DriveFile.update(file._id, { const updated = await DriveFile.update(file._id, {
$set: { metadata: updateQuery } $set: { updateQuery }
}); });
console.dir(updated)
// Serialize // Serialize
const fileObj = await serialize(updated); const fileObj = await serialize(updated);
console.dir(fileObj)
// Response // Response
res(fileObj); res(fileObj);

View file

@ -54,9 +54,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
if (fileId !== undefined) { if (fileId !== undefined) {
file = await DriveFile.findOne({ file = await DriveFile.findOne({
_id: fileId, _id: fileId,
metadata: { 'metadata.user_id': user._id
user_id: user._id
}
}); });
if (file === null) { if (file === null) {

View file

@ -44,9 +44,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// SELECT _id // SELECT _id
const entity = await DriveFile.findOne({ const entity = await DriveFile.findOne({
_id: mediaId, _id: mediaId,
metadata: { 'metadata.user_id': user._id
user_id: user._id
}
}); });
if (entity === null) { if (entity === null) {

View file

@ -44,9 +44,7 @@ const self = (
}); });
const childFilesCount = await DriveFile.count({ const childFilesCount = await DriveFile.count({
metadata: { 'metadata.folder_id': _folder.id
folder_id: _folder.id
}
}); });
_folder.folders_count = childFoldersCount; _folder.folders_count = childFoldersCount;