Я полагаю, что это относительно простой вопрос, но я покопался и пока не смог найти решения. Я недавно изучаю MongoDB / No SQL, и у меня есть файл. js, который я пытаюсь запустить через командную строку, например:
mongo < myscript.js
Но, к сожалению, я получаю синтаксические ошибки (неперехваченные исключение: SyntaxError: неожиданный токен: идентификатор :) вместо успешного вывода в терминал. Я делаю несколько базовых c .insert (), .update (), а затем последующие .find () и .aggregate () для отображения этих данных. Есть ли какой-то синтаксис или форматирование, которые мне нужно изменить при использовании моего кода в качестве скрипта? Выполнение кода в сегментах внутри Mon go Shell дает положительные результаты, все работает успешно. Вот мой. js код:
db = db.getSiblingDB('PurpleBoxDVD');
db.RentalCollection.insertMany(
[
{
"_id": "0000001",
"userID": "PB0000001",
"userFirstName" : "Drew",
"userLastName": "Weidman",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "What was the name of your first pet?",
"Answer1": "Tim"
},
{
"Question2": "What elementary school did you attend?",
"Answer2": "Desert Hills"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "3",
"movieType": "B",
"title": "True Grit",
"actor":
[
{
"actor1FirstName": "John",
"actor1LastName": "Wayne"
},
{
"actor2FirstName": "Glen",
"actor2LastName": "Campbell"
}
],
"directorFirstName": "Henry",
"directorLastName": "Hathaway",
"genre": ["Adventure","Drama","Western"],
"keyword": ["Rooster Cogburn","US Marshal","Oscar Award Winner"]
},
{
"_id": "0000002",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "The Sons of Katie Elder",
"actor":
[
{
"actor1FirstName": "John",
"actor1LastName": "Wayne"
},
{
"actor2FirstName": "Dean",
"actor2LastName": "Martin"
}
],
"directorFirstName": "Henry",
"directorLastName": "Hathaway",
"genre": ["Western"],
"keyword": ["Gun Fighter","Family"]
},
{
"_id": "0000003",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "The Avengers",
"actor":
[
{
"actor1FirstName": "Robert",
"actor1LastName": "Downey Jr"
},
{
"actor2FirstName": "Chris",
"actor2LastName": "Evans"
}
],
"directorFirstName": "Joss",
"directorLastName": "Whedon",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Captain America","Iron Man","Thor"]
},
{
"_id": "0000004",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Greatest Showman",
"actor":
[
{
"actor1FirstName": "Hugh",
"actor1LastName": "Jackman"
},
{
"actor2FirstName": "Michelle",
"actor2LastName": "Williams"
}
],
"directorFirstName": "Michael",
"directorLastName": "Gracey",
"genre": ["Drama","Biography","Musical"],
"keyword": ["Circus","Barnum","Singing","Million Dreams"]
},
{
"_id": "0000005",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Star Wars: Episode IV - New Hope",
"actor":
[
{
"actor1FirstName": "Mark",
"actor1LastName": "Hamill"
},
{
"actor2FirstName": "Carrie",
"actor2LastName": "Fisher"
}
],
"directorFirstName": "George",
"directorLastName": "Lucas",
"genre": ["Fantasy","Adventure","Action"],
"keyword": ["Jedi Knight","Darth Vader","Yoda"]
},
{
"_id": "0000006",
"userID": "PB0000021",
"userFirstName" : "Spencer",
"userLastName": "Hilton",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "P",
"banStatus": "N",
"password": "CSRocks!",
"question":
[
{
"Question1": "What was your first phone number?",
"Answer1": "9999999999"
},
{
"Question2": "What is your mothers maiden name?",
"Answer2": "Johnson"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "3",
"movieType": "B",
"title": "Deadpool",
"actor":
[
{
"actor1FirstName": "Ryan",
"actor1LastName": "Reynolds"
},
{
"actor2FirstName": "Morena",
"actor2LastName": "Baccarin"
}
],
"directorFirstName": "Tim",
"directorLastName": "Miller",
"genre": ["Adventure","Action","Comedy"],
"keyword": ["Mercinary","Morbid","Healing Power"]
},
{
"_id": "0000007",
"userID": "PB0000027",
"userFirstName" : "Josh",
"userLastName": "Jensen",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "S",
"banStatus": "N",
"password": "AndroidIsKing",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Josh Jensen"
},
{
"Question2": "What was your first phone number?",
"Answer2": "8016429755"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "X-Men",
"actor":
[
{
"actor1FirstName": "Hugh",
"actor1LastName": "Jackman"
},
{
"actor2FirstName": "Patrick",
"actor2LastName": "Stewart"
}
],
"directorFirstName": "Bryan",
"directorLastName": "Singer",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Wolverine","Mutants"]
},
{
"_id": "0000008",
"userID": "PB0000101",
"userFirstName" : "Waldo",
"userLastName": "Wildcat",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "S",
"banStatus": "N",
"password": "GoWildcats!",
"question":
[
{
"Question1": "What was the name of your first pet?",
"Answer1": "Thunderbird"
},
{
"Question2": "What elementary school did you attend?",
"Answer2": "Bonneville"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "4",
"movieType": "B",
"title": "The Avengers",
"actor":
[
{
"actor1FirstName": "Robert",
"actor1LastName": "Downey Jr"
},
{
"actor2FirstName": "Chris",
"actor2LastName": "Evans"
}
],
"directorFirstName": "Joss",
"directorLastName": "Whedon",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Captain America","Iron Man","Thor"]
},
{
"_id": "0000009",
"userID": "PB0000101",
"userFirstName" : "Waldo",
"userLastName": "Wildcat",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "S",
"banStatus": "N",
"password": "GoWildcats!",
"question":
[
{
"Question1": "What was the name of your first pet?",
"Answer1": "Thunderbird"
},
{
"Question2": "What elementary school did you attend?",
"Answer2": "Bonneville"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "3",
"movieType": "B",
"title": "Incredibles 2",
"actor":
[
{
"actor1FirstName": "Craig T",
"actor1LastName": "Nelson"
},
{
"actor2FirstName": "Holly",
"actor2LastName": "Hunter"
}
],
"directorFirstName": "Brad",
"directorLastName": "Bird",
"genre": ["Adventure","Action","Animation"],
"keyword": ["Elastigirl","Dash","Jack Jack"]
}
]
);
db.RentalCollection.insertMany(
[
{
"_id": "0000010",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Aug 1 2020",
"returnDate": "null",
"dueDate": "Aug 4 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Deadpool",
"actor":
[
{
"actor1FirstName": "Ryan",
"actor1LastName": "Reynolds"
},
{
"actor2FirstName": "Morena",
"actor2LastName": "Baccarin"
}
],
"directorFirstName": "Tim",
"directorLastName": "Miller",
"genre": ["Adventure","Action","Comedy"],
"keyword": ["Mercinary","Morbid","Healing Power"]
},
{
"_id": "0000011",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Aug 1 2020",
"returnDate": "null",
"dueDate": "Aug 4 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Incredibles 2",
"actor":
[
{
"actor1FirstName": "Craig T",
"actor1LastName": "Nelson"
},
{
"actor2FirstName": "Holly",
"actor2LastName": "Hunter"
}
],
"directorFirstName": "Brad",
"directorLastName": "Bird",
"genre": ["Adventure","Action","Animation"],
"keyword": ["Elastigirl","Dash","Jack Jack"]
},
{
"_id": "0000012",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Aug 1 2020",
"returnDate": "null",
"dueDate": "Aug 4 2020",
"copyNumber": "1",
"movieType": "D",
"title": "X-Men",
"actor":
[
{
"actor1FirstName": "Hugh",
"actor1LastName": "Jackman"
},
{
"actor2FirstName": "Patrick",
"actor2LastName": "Stewart"
}
],
"directorFirstName": "Bryan",
"directorLastName": "Singer",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Wolverine","Mutants"]
},
]
);
db.RentalCollection.find();
db.RentalCollection.updateOne(
{ _id: "0000001"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000002"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000003"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000004"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000005"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000006"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000007"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000008"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000009"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000010"},
{ $set: { returnDate: "Aug 02 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000011"},
{ $set: { returnDate: "Aug 02 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000012"},
{ $set: { returnDate: "Aug 02 2020"}}
);
db.RentalCollection.aggregate(
{ $project: {_id: 0, userFirstName: 1, userLastName: 1, title: 1, rentalDate: 1, actor1FirstName: 1, actor1LastName: 1,actor2FirstName: 1, actor2LastName: 1, directorFirstName: 1, directorLastName: 1, movieType: 1 }}
);
полная ошибка с терминала:
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("775add23-e465-4fe3-a32f-f0ac8a4753cc") }
MongoDB server version: 4.2.8
2020-08-02T10:54:36.056-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:8
2020-08-02T10:54:36.056-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:11
2020-08-02T10:54:36.057-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:8
2020-08-02T10:54:36.057-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:7
2020-08-02T10:54:36.059-0600 E QUERY [js] uncaught exception: SyntaxError: identifier starts immediately after numeric literal :
@(shell):1:8
2020-08-02T10:54:36.059-0600 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '-' :
@(shell):1:2
2020-08-02T10:54:36.059-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:8
2020-08-02T10:54:36.060-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:4
2020-08-02T10:54:36.060-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:13
2020-08-02T10:54:36.061-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:3
2020-08-02T10:54:36.061-0600 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '-' :
@(shell):1:2
2020-08-02T10:54:36.061-0600 E QUERY [js] uncaught exception: SyntaxError: identifier starts immediately after numeric literal :
@(shell):1:8
2020-08-02T10:54:36.062-0600 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '>' :
@(shell):1:0
bye