Skip to content
Snippets Groups Projects
Commit 64be0d6d authored by otofune's avatar otofune
Browse files

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

parent b266ed3e
No related branches found
No related tags found
No related merge requests found
......@@ -14,7 +14,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Calculate drive usage
const usage = ((await DriveFile
.aggregate([
{ $match: { metadata: { user_id: user._id } } },
{ $match: { 'metadata.user_id': user._id } },
{
$project: {
length: true
......
......@@ -40,10 +40,8 @@ module.exports = async (params, user, app) => {
_id: -1
};
const query = {
metadata: {
user_id: user._id,
folder_id: folderId
}
'metadata.user_id': user._id,
'metadata.folder_id': folderId
} as any;
if (sinceId) {
sort._id = 1;
......
......@@ -24,11 +24,9 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Issue query
const files = await DriveFile
.find({
metadata: {
name: name,
user_id: user._id,
folder_id: folderId
}
'metadata.name': name,
'metadata.user_id': user._id,
'metadata.folder_id': folderId
});
// Serialize
......
......@@ -21,9 +21,7 @@ module.exports = async (params, user) => {
const file = await DriveFile
.findOne({
_id: fileId,
metadata: {
user_id: user._id
}
'metadata.user_id': user._id
});
if (file === null) {
......
......@@ -20,19 +20,14 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) return rej('invalid file_id param');
console.dir(user)
// Fetch file
const file = await DriveFile
.findOne({
_id: fileId,
metadata: {
user_id: user._id
}
'metadata.user_id': user._id
});
console.dir(file)
if (file === null) {
return rej('file-not-found');
}
......@@ -42,7 +37,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
if (nameErr) return rej('invalid name param');
if (name) updateQuery.name = name;
if (name) updateQuery['metadata.name'] = name;
// Get 'folder_id' parameter
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 === null) {
updateQuery.folder_id = null;
updateQuery['metadata.folder_id'] = null;
} else {
// Fetch folder
const folder = await DriveFolder
......@@ -63,21 +58,17 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('folder-not-found');
}
updateQuery.folder_id = folder._id;
updateQuery['metadata.folder_id'] = folder._id;
}
}
const updated = await DriveFile.update(file._id, {
$set: { metadata: updateQuery }
$set: { updateQuery }
});
console.dir(updated)
// Serialize
const fileObj = await serialize(updated);
console.dir(fileObj)
// Response
res(fileObj);
......
......@@ -54,9 +54,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
if (fileId !== undefined) {
file = await DriveFile.findOne({
_id: fileId,
metadata: {
user_id: user._id
}
'metadata.user_id': user._id
});
if (file === null) {
......
......@@ -44,9 +44,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// SELECT _id
const entity = await DriveFile.findOne({
_id: mediaId,
metadata: {
user_id: user._id
}
'metadata.user_id': user._id
});
if (entity === null) {
......
......@@ -44,9 +44,7 @@ const self = (
});
const childFilesCount = await DriveFile.count({
metadata: {
folder_id: _folder.id
}
'metadata.folder_id': _folder.id
});
_folder.folders_count = childFoldersCount;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment