I have some json data which i want to send to mongodb using kafka consumer in nodejs. I want different collection for each part of the JSON data. How do i do that? Any help would be appreciated
[
{
"`Bin_details`": {
"Device_ID": "1"
},
"Device_Status": {
"Filled_status": "0.097",
"TimeStamp": "15:56:23"
},
"date": "15-6-2021"
},
{
"Bin_details": {
"Device_ID": "2"
},
"Device_Status": {
"Filled_status": "0.086",
"TimeStamp": "15:56:23"
},
"date": "15-6-2021"
},
{
"Bin_details": {
"Device_ID": "3"
},
"Device_Status": {
"Filled_status": "0.087",
"TimeStamp": "15:56:23"
},
"date": "15-6-2021"
},
You can create a new collection with a loop like this:
const jsonData = [
{
"`Bin_details`": {
"Device_ID": "1"
},
"Device_Status": {
"Filled_status": "0.097",
"TimeStamp": "15:56:23"
},
"date": "15-6-2021"
},
{
"Bin_details": {
"Device_ID": "2"
},
"Device_Status": {
"Filled_status": "0.086",
"TimeStamp": "15:56:23"
},
"date": "15-6-2021"
},
{
"Bin_details": {
"Device_ID": "3"
},
"Device_Status": {
"Filled_status": "0.087",
"TimeStamp": "15:56:23"
},
"date": "15-6-2021"
}
];
// DB Setup
const { MongoClient } = require('mongodb');
const client = new MongoClient(DB_CONNECTION_STRING, { useUnifiedTopology: true });
await client.connect();
const db = await client.db();
// Loop to create collections
let index = 0;
for(let eachData of jsonData){
const collectionName = `Collection-${index}`
await db.createCollection(collectionName);
index++;
}
Related
hello I am trying to update a document on mongodb but everytime I run the lambda function below it does not return the new document but returns a http response 200. I have tried to update the logic in so many forms but I get the response 200 but the update does not happen. The lambda function takes 2 path parameters i.e the userid and the date to find the specific plan then updates it... It neither updates nor returns the new document. Please help!
const Plan = require('../../model/planModel');
const User = require('../../model/userModel');
const { connectDB } = require('../../config/db');
const { verify, decode } = require('jsonwebtoken');
const { decode_token } = require('../../utils');
const { findByIdAndUpdate } = require('../../model/planModel');
exports.handler = async (event) => {
try {
await connectDB();
//console.log(event)
const { userid, date } = event.pathParameters;
const exists = await Plan.findOne(userid, date);
console.log('I made it here: ', exists);
const {
excitedAbout,
gratefulForMorning,
mainThreeTodo,
dailyToDo,
meals,
water,
exercise,
meditation,
relaxation,
happyScale,
happyMoment,
gratefulForTonight,
dailyQuestion,
} = event.body;
const updatedPlan = await Plan.findByIdAndUpdate(
{ _id: exists._id },
{
excitedAbout: excitedAbout,
gratefulForMorning: gratefulForMorning,
mainThreeTodo: mainThreeTodo,
dailyToDo: dailyToDo,
meals: meals,
water: water,
exercise: exercise,
meditation: meditation,
relaxation: relaxation,
happyScale: happyScale,
happyMoment: happyMoment,
gratefulForTonight: gratefulForTonight,
dailyQuestion: dailyQuestion,
},
{ new: true }
);
console.log('this is a plan', updatedPlan);
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(updatedPlan),
};
} catch (error) {
console.log(error);
}
};
This is the data I want to update:
{
"user": "62d1a251806d10095ff0f0a6",
"date": "2022-09-09T18:30:00.000Z",
"excitedAbout": "Excited About something",
"gratefulForMorning": "",
"mainThreeTodo": [
{
"text": "HELLO I survived",
"checked": false
},
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
}
],
"dailyToDo": [
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
},
{
"text": "",
"checked": false
}
],
"meals": {
"breakfast": [
{
"meal": "",
"calories": ""
}
],
"lunch": [
{
"meal": "",
"calories": ""
}
],
"dinner": [
{
"meal": "skuma",
"calories": ""
}
]
},
"water": 7,
"exercise": false,
"relaxation": false,
"meditation": false,
"happyScale": 0,
"happyMoment": "",
"gratefulForTonight": "",
"dailyQuestion": {
"question": "",
"answer": ""
}
}
and this is what I'm getting
{
"meals": {
"breakfast": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c3"
}
],
"lunch": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c2"
}
],
"dinner": [
{
"meal": "",
"calories": "",
"_id": "631cc36ed9e6f6711e7da2c1"
}
]
},
"dailyQuestion": {
"question": "",
"answer": ""
},
"_id": "631ba3e559b8a53d732a1958",
"user": "62d1a251806d10095ff0f0a6",
"date": "2022-09-09T18:30:00.000Z",
"excitedAbout": "",
"gratefulForMorning": "",
"mainThreeTodo": [
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bb"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bc"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bd"
}
],
"dailyToDo": [
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2be"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2bf"
},
{
"text": "",
"checked": false,
"_id": "631cc36ed9e6f6711e7da2c0"
}
],
"water": 0,
"exercise": false,
"meditation": false,
"relaxation": false,
"happyScale": 0,
"happyMoment": "",
"gratefulForTonight": "",
"createdAt": "2022-09-09T20:36:53.680Z",
"updatedAt": "2022-09-13T12:01:02.966Z",
"__v": 0
}
it's like it is never passed
Your query seems to be missing the update operators, that's why it might not be working for you:
const updatedPlan = await Plan.findByIdAndUpdate(
{ _id: exists._id },
{
$set: {
excitedAbout: excitedAbout,
gratefulForMorning: gratefulForMorning,
mainThreeTodo: mainThreeTodo,
dailyToDo: dailyToDo,
meals: meals,
water: water,
exercise: exercise,
meditation: meditation,
relaxation: relaxation,
happyScale: happyScale,
happyMoment: happyMoment,
gratefulForTonight: gratefulForTonight,
dailyQuestion: dailyQuestion,
}
},
{ new: true }
);
Try this, here we have specified the $set update operator.
I need to remove the domain name from the URL in the JSON array stored in Mongo DB using Mongoose.
JSON Array format in DB
{
"_id": {
"$oid": "602a482223df2a16e26f51bc"
},
"message": "Test Again",
"pollType": {
"$numberInt": "1"
},
"type": "TEST",
"optionList": [
{
"name": "name",
"original": "https://name/test/pictures/1613383695668-71393-0d1f829222f945dc",
"thumbnail": "https://name/test/pictures/1613383695668-71393-0d1f829222f945dc",
"vote": {
"$numberInt": "1"
},
"_id": {
"$oid": "602a482223df2a16e26f51b9"
},
"participateUser": [
{
"$oid": "5c9baa00bcafa608891b0b44"
}
],
"latestParticipate": [
{
"$oid": "5c9baa00bcafa608891b0b44"
}
]
},
{
"name": null,
"original": "https://name/test/pictures/1613383695672-71393-e92a34eaf6b48b19",
"thumbnail": "https://name/test/pictures/1613383695672-71393-e92a34eaf6b48b19",
"vote": {
"$numberInt": "0"
},
"_id": {
"$oid": "602a482223df2a16e26f51ba"
},
"participateUser": [
]
},
{
"name": null,
"original": "https://name/test/pictures/1613383695630-71393-3387191491ba279c",
"thumbnail": "https://name/test/pictures/1613383695630-71393-3387191491ba279c",
"vote": {
"$numberInt": "0"
},
"_id": {
"$oid": "602a482223df2a16e26f51bb"
},
"participateUser": [
]
}
],
"pollId": {
"$oid": "602a482223df2a16e26f51b8"
},
"createdAt": {
"$date": {
"$numberLong": "1613383714396"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1613383714396"
}
},
"totalVoteCount": {
"$numberInt": "1"
},
"participateUser": [
{
"$oid": "5c9baa00bcafa608891b0b44"
}
]
}
I am using below code to replace a string
return new Promise(async (resolve, reject) => {
console.log("Iam called")
MongoClient.connect('mongoDBconfig', function (err, client) {
if (err) throw err;
var collection = "test"
var db = client.db('testDB');
db.collection(collection).updateMany({
"optionList.thumbnail": { $regex: /name/ },
}, {
"$set": {
"optionList.$.thumbnail": {
$replaceOne: { input: "optionList.$.thumbnail", find: "https://name/test", replacement: "" },
}
}
}
}, function (error, success) {
if (success) {
resolve(success)
}
else {
reject(error)
}
})
})
})
but when I call this function I getting below error
DB Error : Duplicate Entry : dollar ($) prefixed field '$replaceOne' in 'optionList.0.original.$replaceOne' is not valid for storage.
The $replaceOne is a aggregation operator starting from MongoDB v4.4, you can use update with aggregation pipeline starting from MongoDB v4.2
$map to iterate loop of optionList array
$replaceOne to replace string
$mergeObjects to merge current object with updated original field
let originalName = "https://name/test";
db.collection(collection).updateMany(
{ "optionList.original": { $regex: originalName } },
[{
$set: {
optionList: {
$map: {
input: "$optionList",
in: {
$mergeObjects: [
"$$this",
{
thumbnail: {
$replaceOne: {
input: "$$this.thumbnail",
find: originalName,
replacement: ""
}
}
}
]
}
}
}
}
}],
function (error, success) {
if (success) { resolve(success) }
else { reject(error) }
}
)
Playground
Hi i am new in mongoose and mongodb. I want to remove specific object from the Array in my document and return the updated document. I have tried a lot but it always return null. Here is my document structure.
{
"role": "Student",
"skills": [
"html",
"css",
"js"
],
"_id": "5ef583198e9b23cc8c606c10",
"user": "5ee5c9ef26333935647e54bc",
"__v": 24,
"status": "Intern",
"education": [],
"internships": [
{
"current": false,
"_id": "5ef894d48f601512340f25b5",
"title": "Web",
"company": "asdfadfd",
"from": "2010-02-04T00:00:00.000Z"
},
{
"current": false,
"_id": "5ef894f31dc9413bf89c44d8",
"title": "Django",
"company": "example",
"from": "2010-02-04T00:00:00.000Z"
}
]
}
And here is my updating function
exports.deleteStudentInternship = async (req, res, next) => {
const deleteInternship = await Student.findOneAndUpdate(
{ $and: [{ user: req.user.id }, { 'internships': { $elemMatch: { _id: req.params.intern_id } } }] },
{ '$pull': { 'internships': { _id: req.params.intern_id } } },
{
new: true,
useFindAndModify: false,
},
function (error) {
if (error) return validationError(404, { internship: 'Internship id not exist' }, next)
}
);
if (!deleteInternship) {
return validationError(404, { internship: 'Internship id not exist' }, next)
}
res.status(200).json(deleteInternship);
}
Please change the pull part I mean
{ '$pull': { 'internships': { _id: req.params.intern_id } } }
to this and try:
{ '$pull': { 'internships': req.params.intern_id } }
if anyone can hazard a guess or where to look it would be greatly appreciated.
I can get nested data when I run using graphgl API, however, from my node program it only shows top-level items - does not display the nested elements for the customer and lineitem object.
I am using Koa middle where, with promise response:
router.get('/orders/', async (ctx) => {
const auth = prepareAuth(ctx);
await getOrders(auth).then(response => ctx.body = response.data.data.orders);
console.log(ctx.body.edges)
However from the console it has (customer null and 'object':
[
{
node: {
createdAt: '2020-02-24T12:53:20Z',
customer: null,
name: '#1001',
lineItems: [Object]
}
},
{
node: {
createdAt: '2020-02-24T12:53:50Z',
customer: null,
name: '#1002',
lineItems: [Object]
}
},
{
node: {
createdAt: '2020-03-10T21:11:04Z',
customer: null,
name: '#1003',
lineItems: [Object]
}
}
]
when i use the GraphQL API directly the query works fine and I get full response:
{
"data": {
"orders": {
"edges": [
{
"node": {
"createdAt": "2020-02-24T12:53:20Z",
"customer": {
"displayName": "franko girl"
},
"name": "#1001",
"lineItems": {
"edges": [
{
"node": {
"name": "dance mat red",
"quantity": 4
}
}
]
}
}
},
{
"node": {
"createdAt": "2020-02-24T12:53:50Z",
"customer": {
"displayName": "franko man"
},
"name": "#1002",
"lineItems": {
"edges": [
{
"node": {
"name": "dance mat black",
"quantity": 2
}
}
]
}
}
},
{
"node": {
"createdAt": "2020-03-10T21:11:04Z",
"customer": {
"displayName": "franko man"
},
"name": "#1003",
"lineItems": {
"edges": [
{
"node": {
"name": "dance mat black",
"quantity": 1
}
},
{
"node": {
"name": "dance mat red",
"quantity": 1
}
}
]
}
}
}
]
}
},
Okay, so finally figured this out, for anyone else who stumbles accross this problem, you need to the convert the json object to a string using built in javascript function: JSON.stringify()
from W3schools.com
var obj = { name: "John", age: 30, city: "New York" };
var myJSON = JSON.stringify(obj);
I have collection trusted contacts. I need to filter the document by the user. When I use method findOne I have a result, but when I use $match I got an empty array. I don't know why $match doesn't work for me.
Collection trusted contacts:
{
"_id": {
"$oid": "5d76008e4b98e63e58cb34cc"
},
"date": {
"$date": "2019-09-09T07:32:20.174Z"
},
"approvedTrustedContacts": [
{
"_id": {
"$oid": "5d764e411b7476462cf6b540"
},
"user": {
"$oid": "5c5ecaf6134fc342d4b1a9d5"
}
},
{
"_id": {
"$oid": "5d7750af52352918f802c474"
},
"user": {
"$oid": "5c64968cae53a8202c963223"
}
}
],
"pendingApprovalContacts": [],
"waitingForApprovalContacts": [],
"user": {
"$oid": "5d76008e4b98e63e58cb34cb"
}
},
{
"_id": {
"$oid": "5d7605f5e7179a084efa385b"
},
"date": {
"$date": "2019-09-09T07:32:20.174Z"
},
"approvedTrustedContacts": [
{
"_id": {
"$oid": "5d764e411b7476462cf6b541"
},
"user": {
"$oid": "5d76008e4b98e63e58cb34cb"
}
}
],
"pendingApprovalContacts": [],
"waitingForApprovalContacts": [],
"user": {
"$oid": "5c5ecaf6134fc342d4b1a9d5"
}
}
when I use method findOne
const user = await TrustedContacts.findOne({ user: "5d76008e4b98e63e58cb34cb" })
I have result
but when I use $match I got empty array
result1 = await TrustedContacts.aggregate([
{ $match: { user: "5d76008e4b98e63e58cb34cb" } },
]);
It Works,
const ObjectId = require('mongodb').ObjectId;
result1 = await TrustedContacts.aggregate([
{ $match: { user: ObjectId("5d76008e4b98e63e58cb34cb") } },
]);