while using mongoose insertMany to populate a collection I noticed it's only saving the object id and version for each data in the array as such
but my model and data are structured as shown in code below
//SCHEMA MODEL
const { Schema, model } = require("mongoose");
const staffSchema = new Schema({
Phone: { String },
Timestamp: { String },
Fullname: { String },
});
module.exports = { staffConvert: model("staff", staffSchema) };
though my actual data is coming from converting excel to json but this a sample of the data
//SAMPLE DATA ARRAY
const data = [
{
Phone: "92893249",
Timestamp: "8/24/2020 10:08:25",
Fullname: "Musa Yahaya",
},
{
Phone: "92893249",
Timestamp: "8/24/2020 10:08:25",
Fullname: "Musa Yahaya",
},
];
try {
if (data.length === 0) return console.log("No data to upload");
await staffConvert
.insertMany(data)
.then((docs) => {
console.log(
"success_msg",
"Staffs record uploaded successfully" + " " + docs
);
// req.flash("success_msg", "Staffs record uploaded successfully");
})
.catch((error) => {
console.log(
"Staffs record uploaded failed" + " " + error.message
);
});
} catch (error) {
console.log("Staffs record uploaded failed" + " " + error.message);
// req.flash(
// "error_msg",
// "Staffs record uploaded failed" + " " + error.message
// );
}
You have to make change to your model. You're defining your attribute as object type. If you want to define it like that, you have to explicitly tell mongo that it's string type not object.
const staffSchema = new Schema({
Phone: String,
Timestamp: { type: String },
});
This should solve your issue
Related
can someone help me with dialogflow cx webhooks and add data to Firestore, this is my index.js code but it does not store in the database. Thanks
Hello, can someone help me with dialogflow cx webhooks and add data to Firestore, this is my index.js code but it does not store in the database. Thanks
const Firestore = require('#google-cloud/firestore');
const PROJECTID = 'ia-proyecto';
const firestore = new Firestore({
projectId: PROJECTID,
timestampsInSnapshots: true,
});
exports.db_datos_personales = (req, res) => {
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
console.log('Dialogflow Request body: ' + JSON.stringify(req.body));
let tag = req.body.fulfillmentInfo.tag;
console.log('Tag: ' + tag);
console.log('Session Info Parameters: ' + JSON.stringify(req.body.sessionInfo.parameters));
//
if (tag === 'nombre') {
const COLLECTION_NAME = 'Datos_Personales';
let Nombre_Usuario = replaceAll(JSON.stringify(req.body.sessionInfo.parameters['nombre_usuario']), '"', '');
let Ciudad_Usuario = replaceAll(JSON.stringify(req.body.sessionInfo.parameters['ciudad_usuario']), '"', '');
console.log('Nombre usuario: ' + Nombre_Usuario);
console.log('Ciudad usuario: ' + Ciudad_Usuario);
const data = {
Nombre_Usuario: Nombre_Usuario,
Ciudad_Usuario: Ciudad_Usuario,
};
console.log(JSON.stringify(data));
var answer = 'Welcome to the Cloud Audio family, '+ Nombre_Usuario +'! Enjoy our services.';
return firestore.collection(COLLECTION_NAME)
.set(data)
.then(doc => {
return res.status(200).send({
fulfillmentResponse: {
messages: [{
text: {
text: [answer]
}
}]
}
});
}).catch(err => {
console.error(err);
return res.status(404).send({ error: 'unable to store', err });
});
//
} else if (tag === 'pregunta') {
const COLLECTION_NAME = 'Pregunta_Usuario';
let pregunta_usuario = replaceAll(JSON.stringify(req.body.sessionInfo.parameters['pregunta_usuario']), '"', '');
console.log('Pregunta Usuario: ' + pregunta_usuario);
const data = {
pregunta_usuario: pregunta_usuario
};
console.log(JSON.stringify(data));
var answer = 'Your plan has been changed to the '+ pregunta_usuario + ' service.';
return firestore.collection(COLLECTION_NAME)
.doc(phone_number)
.update(data)
.then(doc => {
return res.status(200).send({
fulfillmentResponse: {
messages: [{
text: {
text: [answer]
}
}]
}
});
}).catch(err => {
console.error(err);
return res.status(404).send({ error: 'unable to update field', err });
});
}
};
I hope to have a solution, since I was more than a week and I can't find anything.
i'm stuck at displaying an image that i uploaded to mongodb with multer-gridfs-storage.
the problem is, i'm supposed to have binary data in every chunks and i want to join them to make the final file and encode it to base64
mongodb tell it's binary data but i'm not very sure
mongodb chunks
here is my code:
multer.js:
const multer = require("multer");
const { GridFsStorage } = require("multer-gridfs-storage");
const db = require("../utils/database").db;
const storage = new GridFsStorage({ db });
module.exports = {
upload: multer({
storage: storage,
})
};
my function to reassemble the file:
const getImage = (fileName) => {
return files
.find({ filename: fileName })
.then((file) => {
const id = mongoose.Types.ObjectId(file[0]._id);
return chunks
.find({ files_id: id })
.then((chunks) => {
if (!chunks || chunks.length === 0) {
console.log("No data found");
}
let fileData = [];
for (let i = 0; i < chunks.length; i++) {
//This is in Binary JSON or BSON format, which is stored
//in fileData array in base64 endocoded string format
fileData.push(chunks[i].data.toString('base64');
}
//Display the chunks using the data URI format
return (finalFile =
"data:" + file[0].contentType + ";base64," + fileData.join(""));
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
};
and i have this kind of result with .toString('base64')
���R3\x17�\x18��bC4%\x19�rS5&��cDT6\'7\x1A�s�E���dUte�89\x11\x00\x02\x01\x02\x04\x03\x05\x07\x02\x04\x04\x05\x02\x03\x01\x11\x00\x01\x02\x11\x03!1\x12\x04AQ\x05aq"\x13\x06������2�\x07�B#\x14�R3\x15br$4\b�\x16���C%�S\x17D5&�c��s�Td6\t��\x00\f\x03\x01\x00\x02\x
and without .toString('base64')
77+977+977+9DR7vv70D0IJO77+9NSVq0qEu77+9I++/vRl477+9D++/vS3vv70GUE7vv71H77+977+9UO+/vS8eKO+/vVLvv73vv73vv73vv73vv70d77+9XO+/vXjvv71t77+977+9f++/vW3vv73vv73vv73vv71Q77+977+9Nu+/vSVD77+977+977+9FXnvv70277+9d++/vRE3Zu+/vQcgST1P77+92orVle+/vX5lIu+/vVPvv70o77+9Cu+/ve+/vTvvv73bnu+/vR3vv70a77+9VEZqTu+/vXkR77+977+9cyjvv73vv70kVT/vv73vv73vv70Hbe+/vW3vv73oiJzvv71FeGtS77+9Xu+/ve+/vT9xWcSGAO+/ve+/ve+/ve+/ve+/vX3vv73vv73vv73vv73vv71zSsKcVF5VOw7vv73vv73vv71M77+9CGPvv73Mo++/ve+/vWDvv712Ie+/ve+/vXbvv71g77+9S++/ve+/vWJRWNGU77+977+9GSw5AO+/vRIzdu+/ve+/vQkb77+9Se+/vTrvv70277+977+977+977+977+977+9RVVn77
thanks for your help !
Ok, i figured out what was the problem,
in my model chunks.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const chunks = new Schema({
files_id: {
type: mongoose.Schema.Types.ObjectId,
},
n: {
type: Number,
},
data: {
type: String,
},
});
module.exports = mongoose.model("fs.chunks", chunks);
i had the type of data in 'String" instead of "Buffer"
it just work as expected with this
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const chunks = new Schema({
files_id: {
type: mongoose.Schema.Types.ObjectId,
},
n: {
type: Number,
},
data: {
type: Buffer,
},
});
module.exports = mongoose.model("fs.chunks", chunks);
How can I execute this populate so that I can get the username of the person that does the tweet? I've tried with the function getusername() but it is not working as it gives me a tweetsSchema.findOne is not a function error.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var tweetsSchema = new Schema(
{
tweets: { type: String, required: true }, //reference to the associated book
replies: { type: String, required: false },
username: { type: Schema.Types.ObjectId, ref: 'users' }
}
);
// Virtual for bookinstance's URL
function getUsername(tweets){
return tweetsSchema.findOne({ tweets: tweets })
.populate('username').exec((err, posts) => {
console.log("Populated User " + username);
})
}
getUsername()
//Export model
module.exports = mongoose.model('tweets', tweetsSchema);
currently the function you have defined is just a function and haven't defined on the Schema of yours and not exported.
you might want to use static in mongoose
tweetsSchema.static('getUsername', async function(tweets){
return await tweetsSchema.findOne({ tweets: tweets })
.populate('username').exec((err, posts) => {
console.log("Populated User " + username);
})
})
I have such code where I can get new inserted doc id:
db.collection("cities").add({
name: "Tokyo",
country: "Japan"
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
Is it possible to get data from ref?
Yes you can do it like this:
.then(function(docRef) {
db.collection("cities").doc(docRef.id).get()
.then(snap => {
console.log('Here is the document you wrote to', snap.data()
})
})
Granted it requires another read which you probably didn't want
I found solution
static async createUserPofile(uid) {
const db = Firebase.firestore(),
date = Firebase.firestore.FieldValue.serverTimestamp(),
data ={
user_id: uid,
creation_time: date,
update_time: date
},
docRef = await db.collection("user_profiles").add(data),
get = await docRef.get();
return get.data();
}
We could retrieve added or updated data using this function:
function dataToObject(fDoc){
return { ...fDoc.data(), id: fDoc.id }}
We should chain 'get' method to retrieve added or updated firestore data:
let fDoc = await db.collection("cities").add({
name: "Tokyo",
country: "Japan"
}).get()
Use it to transform your snapshot to an object:
const upserData = dataToObject(fDoc)
I am trying to access a document and get the results to console. Mongoose debug shows that there is a document, yet returns an empty record.
//mongoose connect to db
var db = mongoose.createConnection('mongodb://localhost/mean-dev1/', function(err) {
if (err) throw err;
});
db.once('open', function callback() {
var tripSchema = mongoose.Schema(
{ title: String,
fromDate: Date,
tillDate: Date,
price: Number,
details: String,
availability: Number
});
// Model definition
var TripObj = db.model('TripObj', tripSchema, 'tripmodel');
TripObj.find(function(err, data) {
if (!err){
console.log('%s', data); //gets undefined
console.log("written...", data);
}else{
throw err;
process.exit();
}
});
}
and here is the console output
...
Migrations: Up to date at version 003
Mongoose: tripmodel.find({}) { fields: { title: 1 } }
is undefined?
written... []
Figured it out, the problem was with my initial connection. So changed the code a bit.
//connects to mongoose datasource and populates the data.json file in app
mongoose.disconnect();
mongoose.connect('mongodb://localhost/tripDB/', function(err) {
if (err) throw err;
}),
Schema = mongoose.Schema
var db = mongoose.connection;
db.once('open', function callback() {
var tripSchema = mongoose.Schema(
{
availability: Number,
details: String,
fromDate: Date,
price: Number,
tillDate: Date,
title: {type:String, ref:'TripObj'}
});
// Model definition
var TripObj = mongoose.model('TripObj', tripSchema, 'trips');
TripObj.find(function(err, data) {
if (!err){
var newdata = data;
//console.log('%s is undefined?', newdata);
fs.writeFileSync(path.join(__dirname, '../../../../' + 'content/themes/traveller/assets/js' + '/data.json'), JSON.stringify(newdata));
console.log("written succesfully", data);
process.exit();
}else{
throw err;
}
});
});