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

This commit is contained in:
otofune 2017-11-06 16:22:18 +09:00
parent 50b5680389
commit 02429277ff
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
const usage = ((await DriveFile
.aggregate([
{ $match: { metadata: { user_id: user._id } } },
{ $match: { 'metadata.user_id': user._id } },
{
$project: {
length: true

View File

@ -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;

View File

@ -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

View File

@ -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) {

View File

@ -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);

View File

@ -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) {

View File

@ -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) {

View File

@ -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;