db.students.insertOne({
_id: 1001,
Firstname: "John",
Lastname: "smith",
Address: {
Streetaddress: "123 Monash Drive",
Suburb: "Clayton",
State: "VIC",
Postcode: 3168,
},
Gender: "Male",
Course: "BITS",
Year: 2019,
"Off-Campus": "false",
Email: ["jsmith#gmail.com", "jsmith#yahoo.com"],
});
After type these, i got the error of uncaught exception: SyntaxError: missing : after property id :.
Idk why this happen.
The document which you are trying to insert isn't a valid JSON, try with the following -
db.students.insertOne({
"_id": 1001,
"Firstname": "John",
"Lastname": "smith",
"Address": {
"Streetaddress": "123 Monash Drive",
"Suburb": "Clayton",
"State": "VIC",
"Postcode": 3168
},
"Gender": "Male",
"Course": "BITS",
"Year": 2019,
"Off-Campus": "false",
"Email": [
"jsmith#gmail.com",
"jsmith#yahoo.com"
]
});
Related
How do i a write a query that will copy the phoneNumber from data.accounts.contact.phone.phoneNumber to data.phoneNumber in jsonb postgres.
I tried this command
UPDATE customer."user" SET domain = jsonb_set(domain,'{phoneNumber}', text('domain' ->'accounts'-> 0 -> 'contacts' -> 'phone' -> 'phoneNumber'))
but got this error
Error: HINT: Could not choose a best candidate operator. You might need to add explicit type casts.
SQL state: 42725
Character: 1913
{
"id": "87b31b1f-5dae-4506-8099-9812fa1633eb",
"gender": "F",
"status": "VERIFIED",
"lastName": "Lawal",
"password": "T3m1t0p3",
"username": "aminat2#gmail.com",
"firstName": "Aminat",
"phoneNumber": "",
"accounts": [
{
"status": "IN_REVIEW",
"contact": {
"phone": { "phoneNumber": "7809284029", "diallingCode": "+44" },
"address": { "city": "London", "address": "42 Sark Walk", "country": "United Kingdom", "postcode": "E163PS" },
"emailAddress": "aminat2#gmail.com"
},
"location": {
"id": "4a110b1f-9319-4282-b645-81ea71b53e04",
"status": "ACTIVE",
"currency": {
"id": "1",
"to": false,
"date": "2021-09-19T16:45:33",
"from": true,
"buyFxRate": "1",
"sellFxRate": "1",
"
},
"diallingCode": "+44",
"locationLabel": "United Kingdom",
"
"modifiedDateTime": "2021-09-19T16:45:33",
},
}
],
}
Here are my schemas:
1.The Records schema:
const mongoose = require('mongoose')
const RecordsSchema = new mongoose.Schema({
Title: { type: String, required: true },
postedby: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
}],
Author: { type: String, required: true },
ISBN: { type: String, required: true },
Review: { type: String },
SelectedFile: { type: String },
Likes: { type: Number, default: 0 },
Date: { type: Date, default: Date.now() }
});
module.exports = Records = mongoose.model('record', RecordsSchema, 'record');
Here is the The user Schema:
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema({
username: { type: String },
email: { type: String, required: true, unique: true },
records: [{
type: [mongoose.Schema.Types.ObjectId],
ref: 'record'
}],
password: { type: String, required: true },
Date: { type: Date, default: Date.now(), immutable: true }
});
module.exports = User = mongoose.model('user', userSchema, 'user');
The express route for getting a record:
router.get('/postedby/', (req, res) => {
Records.findOne()
.populate('postedby')
.exec()
.then(post => {
if (!post) {
return res.status(400).json({ msg: 'Add Posts' });
}
else return res.json(post);
}).catch(err => console.error(err))
});
Result of the route:
{
"postedby": [],
"Likes": 0,
"_id": "5fed8c12a4fb2c1e98ef09f6",
"Title": "New Age",
"Author": "Situma Prisco",
"ISBN": "23422",
"SelectedFile": "",
"Review": "",
"Date": "2020-12-31T08:30:10.321Z",
"__v": 0
},
I'm getting a blank Array on the populated user field(posteddby) .
Please help, What am I doing wrong? And yes, i do have a User Logged in
I implemented your code and Schemas, It's work for me, if you stored data correctly in the database, every things will be ok...
note :
please use find({}) instead of findOne() if data is not displayed again for postedby key, Other issues need to be addressed like mongoose version and ..., because your code and schemas is correct
it's result for me with find({}):
[
{
"postedby": [
{
"records": [
"5ff6bc0bd9e7437184b83f76",
"5ff6bc23d9e7437184b83f78",
"5ff6bc85f1045a4f102bc0cd",
"5ff6bca1f1045a4f102bc0ce",
"5ff6bca5f1045a4f102bc0cf",
"5ff6bcb3f1045a4f102bc0d0",
"5ff6bcc7f1045a4f102bc0d1"
],
"_id": "5ff6b81df463322abc7406ec",
"Date": "2021-01-07T07:28:15.654Z",
"email": "a#test.com",
"password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
"__v": 7
}
],
"Likes": 1,
"Date": "2021-01-07T07:47:11.610Z",
"_id": "5ff6bca1f1045a4f102bc0ce",
"Title": "farsi",
"Author": "javid1",
"ISBN": "1",
"Review": "1",
"SelectedFile": "1",
"__v": 0
},
{
"postedby": [
{
"records": [
"5ff6bc0bd9e7437184b83f76",
"5ff6bc23d9e7437184b83f78",
"5ff6bc85f1045a4f102bc0cd",
"5ff6bca1f1045a4f102bc0ce",
"5ff6bca5f1045a4f102bc0cf",
"5ff6bcb3f1045a4f102bc0d0",
"5ff6bcc7f1045a4f102bc0d1"
],
"_id": "5ff6b81df463322abc7406ec",
"Date": "2021-01-07T07:28:15.654Z",
"email": "a#test.com",
"password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
"__v": 7
}
],
"Likes": 1,
"Date": "2021-01-07T07:47:11.610Z",
"_id": "5ff6bca5f1045a4f102bc0cf",
"Title": "farsi",
"Author": "javid1",
"ISBN": "1",
"Review": "1",
"SelectedFile": "1",
"__v": 0
},
{
"postedby": [
{
"records": [
"5ff6bc0bd9e7437184b83f76",
"5ff6bc23d9e7437184b83f78",
"5ff6bc85f1045a4f102bc0cd",
"5ff6bca1f1045a4f102bc0ce",
"5ff6bca5f1045a4f102bc0cf",
"5ff6bcb3f1045a4f102bc0d0",
"5ff6bcc7f1045a4f102bc0d1"
],
"_id": "5ff6b81df463322abc7406ec",
"Date": "2021-01-07T07:28:15.654Z",
"email": "a#test.com",
"password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
"__v": 7
}
],
"Likes": 1,
"Date": "2021-01-07T07:47:11.610Z",
"_id": "5ff6bcb3f1045a4f102bc0d0",
"Title": "riyazi",
"Author": "javid1",
"ISBN": "1",
"Review": "1",
"SelectedFile": "1",
"__v": 0
},
{
"postedby": [
{
"records": [
"5ff6bc0bd9e7437184b83f76",
"5ff6bc23d9e7437184b83f78",
"5ff6bc85f1045a4f102bc0cd",
"5ff6bca1f1045a4f102bc0ce",
"5ff6bca5f1045a4f102bc0cf",
"5ff6bcb3f1045a4f102bc0d0",
"5ff6bcc7f1045a4f102bc0d1"
],
"_id": "5ff6b81df463322abc7406ec",
"Date": "2021-01-07T07:28:15.654Z",
"email": "a#test.com",
"password": "$2a$12$wJVDysQxbjuRve.hYn/lbO0rskhwj6y8lwDuEWpCwHeNT/V2mybs.",
"__v": 7
}
],
"Likes": 1,
"Date": "2021-01-07T07:47:11.610Z",
"_id": "5ff6bcc7f1045a4f102bc0d1",
"Title": "zaban",
"Author": "javid1",
"ISBN": "1",
"Review": "1",
"SelectedFile": "1",
"__v": 0
},
{
"postedby": [
{
"records": [
"5ff6c275964d062f045e93d3",
"5ff6c283964d062f045e93d5"
],
"_id": "5ff6c253964d062f045e93d2",
"Date": "2021-01-07T08:01:21.499Z",
"email": "b#test.com",
"password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.",
"__v": 2
}
],
"Likes": 1,
"Date": "2021-01-07T08:01:21.990Z",
"_id": "5ff6c275964d062f045e93d3",
"Title": "zaban",
"Author": "javid1",
"ISBN": "1",
"Review": "1",
"SelectedFile": "1",
"__v": 0
},
{
"postedby": [
{
"records": [
"5ff6c275964d062f045e93d3",
"5ff6c283964d062f045e93d5"
],
"_id": "5ff6c253964d062f045e93d2",
"Date": "2021-01-07T08:01:21.499Z",
"email": "b#test.com",
"password": "$2a$12$jmHUrTSPwjaVd0VEIpsGauExHNSukHRyWWiJt4UlEgeWLBo8GPDH.",
"__v": 2
}
],
"Likes": 1,
"Date": "2021-01-07T08:01:21.990Z",
"_id": "5ff6c283964d062f045e93d5",
"Title": "tttt",
"Author": "javid1",
"ISBN": "1",
"Review": "1",
"SelectedFile": "1",
"__v": 0
}
]
i want to filter the result as the following in mongodb. I use $lookup to populate the result from another collection. Please check my following code
This code below is what i get
{
"_id": "5f3d563122de0730d0f6a754",
"barcode": "1234",
"productname": "Lays Packet",
"brandName": "Lays",
"productSize": "12",
"price": "12",
"quant": "12",
"imageurl": "http://localhost:3000/images/imageurl-1597855281050.jpg",
"remaining": "12",
"creator": "3d943b957fb5db510d824c5cbd6e8f7d",
"__v": 0,
"source": [
{
"_id": "5f3a9bbc325a074240a1a815",
"firstname": "test",
"lastname": "test",
"storename": "test",
"gst": "test",
"phoneNumber": 1,
"email": "1#demo.com",
"address1": "test",
"address2": "test",
"city": "test",
"state": "test",
"country": "test",
"zip": "1",
"password": "1",
"usertype": 3,
"unique_SHOP": "3d943b957fb5db510d824c5cbd6e8f7d",
"__v": 0
}
]
},
How to retrieve only unique_SHOP and zip from source listing.I want result like the one below with one or more fields
{
"_id": "5f3d563122de0730d0f6a754",
"barcode": "1234",
"productname": "Lays Packet",
"brandName": "Lays",
"productSize": "12",
"price": "12",
"quant": "12",
"imageurl": "http://localhost:3000/images/imageurl-1597855281050.jpg",
"remaining": "12",
"creator": "3d943b957fb5db510d824c5cbd6e8f7d",
"__v": 0,
"source": [
{
"zip": "1",
"unique_SHOP": "3d943b957fb5db510d824c5cbd6e8f7d",
}
]
},
The query i use
List.aggregate([
{$match:
{ productname: { $regex: req.params.query,$options: "i" }}
},
{ $lookup:{
from: "suppliers",
localField: "creator",
foreignField: "unique_SHOP",
as: "source"
}
},
])
You can try $lookup with pipeline,
$match condition of creator id
$project to display required fields
{
$lookup: {
from: "suppliers",
as: "source",
let: { creator: "$creator" },
pipeline: [
{
$match: {
$expr: { $eq: ["$$creator", "$_id"] }
}
},
{
$project: {
_id: 0,
zip: 1,
unique_SHOP: 1
}
}
]
}
}
Playground
I'm trying to use a JSON file to populate my DB - I've had success with some other files, but this file / model is throwing validation errors and the errors aren't very helpful to me - They say there is a validation error, they even kinda tell me that it's at a forEach line in the schematype.js file - but I'm not gonna go and change a file in the node-modules folder...especially when I think it's my coding fault.
"mongoose": "4.9.8"
This is in the APP.JS - which calls /loaddata:
app.get('/api/loadData', (req, res) => {
var Xs = require( './_mock_data/x.json' );
for ( var i in Xs ) {
X.create( Xs[i] );
console.log( Xs[i] );
};
});
This is in my model page:
const XSchema = mongoose.Schema({
name:{
type: String,
required: true
},
state:{
type: String
},
type:{
type: String
},
people:{
type: [mongoose.Schema.Types.Mixed]
},
create_date:{
type: Date,
default: Date.now
}
});
My JSON file looks like this - I think the offending data is the "people" with a Mixed type - my confusion comes with the fact that it IS an array of people, so the forEach should loop it - why is it failing?
[
{
"name": "Building One",
"state": "Alaska",
"people": [{
"position": "Chair",
"department": "Sales",
"email": "aftcs#blah.io",
"phone": "",
"name": "John Smith"
}],
"type": "Business"
}, {
"name": "Fairbanks Folly",
"state": "Maine",
"people": [{
"position": "Sitting",
"department": "AAA",
"email": "ffjda#uafert.com",
"phone": "",
"name": "Jane Aspnes"
}, {
"position": "Standing",
"department": "BBB",
"email": "jmbicigo#Maine.com",
"phone": "",
"name": "James Bicigo"
}, {
"position": "Lying",
"department": "AAA",
"email": "kmbutlerhopkins#Craf.org",
"phone": "",
"name": "Kam Butler-Hopkins"
}],
"type": "Other"
}
]
And lastly - the ERROR -
Unhandled rejection ValidationError: College validation failed
at new ValidationError (C:\MyProject\node_modules\mongoose\lib\error\validation.js:23:11)
at model.Document.invalidate (C:\MyProject\node_modules\mongoose\lib\document.js:1629:32)
at C:\MyProject\node_modules\mongoose\lib\document.js:1501:17
at validate (C:\MyProject\node_modules\mongoose\lib\schematype.js:733:7)
at C:\MyProject\node_modules\mongoose\lib\schematype.js:778:11
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (C:\MyProject\node_modules\mongoose\lib\schematype.js:738:19)
at C:\MyProject\node_modules\mongoose\lib\document.js:1499:9
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Again, any help and insight is much appreciated!
//parmeter
var params = {
"auth":auth,
"kind": "reseller#customer",
"customerId": "customerId",
"customerDomain": "customer domain",
"postalAddress": {
"kind": "customers#address",
"contactName": "John Doe",
"organizationName": "Example Inc",
"postalCode": "94043",
"countryCode": "US",
},
"alternateEmail": "email address "
}
//using api
var service = google.reseller('v1');
service.customers.insert(params,function(err,data){
console.log(err);
console.log(data);
})
Iam getting this error:
{ [Error: Invalid Value]
code: 400,
errors: [ { domain: 'global', reason: 'invalid', message: 'Invalid Value' } ] }
You can use request module to perform this action in easy way.
here is an example to insert a customer execute it
POST https://www.googleapis.com/apps/reseller/v1/customers
Body parameter
{
"kind": "reseller#customer",
"customerId": "custId-1234",
"customerDomain": "example.com",
"postalAddress": {
"kind": "customers#address",
"contactName": "John Doe",
"organizationName": "Example Inc",
"postalCode": "94043",
"countryCode": "US",
},
"alternateEmail": "alternateEmail#google.com"
}
For more reference
The problem is stated in your error logs already.
reason: message: 'Invalid Value'
"auth":auth is not included in the Customers resource.
These are the valid values:
{
"kind": "reseller#customer",
"customerId": string,
"customerDomain": string,
"postalAddress": {
"kind": "customers#address",
"contactName": string,
"organizationName": string,
"locality": string,
"region": string,
"postalCode": string,
"countryCode": string,
"addressLine1": string,
"addressLine2": string,
"addressLine3": string
},
"phoneNumber": string,
"alternateEmail": string,
"resourceUiUrl": string,
"customerDomainVerified": boolean
}