What is the proper way to insert data into mongodb? I am able to see other collection by nodejs api call but if i use below method data are inserted but i am not able to see the data on my nodejs api call.
So How to resolve this isse?
Here is my data:
db.trade.insert([
{
"p_id":"ot1",
"product_name":"Mixed",
"product_weight":"1kg",
"product_price":550,
"product_image":"rose.jpg"
},
{
"p_id":"ot2",
"product_name":"Mixed",
"product_weight":"1kg",
"product_price":550,
"product_image":"rose.jpg"
},
{
"p_id":"ot3",
"product_name":"Mixed",
"product_weight":"1kg",
"product_price":550,
"product_image":"rose.jpg"
}
]);
the insert() function takes a single document, while insertMany() takes an array of documents.
In your code, you're attempting to pass an array (multiple documents) into insert() (which expects a single document).
You could either make multiple calls to insert() or a single call to insertMany().
Related
We have a document that needs to be transformed into a different structure document and to be saved in a different mongo database.
How can I do that?
This must be done via a script.
You can construct new documents based on old ones by the use of field references. We can use literals like $mergeObjects.
{
"newfield1" "$oldDocField1"
"newfield2" "$oldDocField2"
{
"newfield3" "$oldDocField3"
}
}
I have to get the values of parameter by calling REST API. I need to fetch the parameters from database for which I need to get the value.
How can I pass the parameters from database.
You could use a Lookup activity to first query your database and get whatever value you want to get. You will be able to access this returned value in the lookup in your next steps. Parameterize your REST API linked service/dataset. You can pass in the value returned by the Lookup activity to this dataset parameter.
First, check if you are using firstRowOnly or your lookup is returning multiple rows. If it is returning multiple rows, you need to keep your next step in a forEach loop.
If your lookup activity is returning two rows, you get the output like shown below.
{
"count": 2,
"value": [
{
"enrollment_number": "123445"
},
{
"enrollment_number": "345678"
}
]
}
ADF Expression for URL would be this:
RemainingUrl/#{activity(activity_name).output.value.enrollment_number}
I want to insert one document into collection1 and after successfully inserting the document, I want to insert another document into collection2. One of the fields for the document in collection2 will be the _id of the document just inserted into collection1.
I am using a callback:
db.collection1.insert(<document>,function(err,doc)){
db.collection2.insert({collection1_id: doc[0]._id, <field>:<value>})
However, it seems that callback is not available without Node.js.
Is there any workaround?
Callbacks are part of the Node.js async API and are not supported in the mongo shell (as at MongoDB 4.0). However, you can always write the equivalent without callbacks.
The mongo shell's insertOne() method will return an insertedId field with the _id value of the inserted document, so you can either save or reference this value.
For example:
db.collection2.insertOne({
collection1_id: db.collection1.insertOne({}).insertedId,
field: 'value'
})
I have a MongoDb ATLAS database as "BOULDERS_MAIN" and a collection inside it "users", I am writing a webhook function for returning a user based on email i.e. "kb", but all I'm getting is empty object.
MongoDB's find() returns a cursor object not the results. Try calling toArray() on the result instead:
var doc = users.find({email: 'kb'}).toArray();
I'm trying to find a way to achieve the following pseudo function on the server.The fields likesRecieved likesShown and likesMatch in exist in a document within the Posts collection.
I require this function to perform for every document in post collection by default. This is because Id like the function to do this...
1) find value(s) that exist in likesRecieved and likedShown fields.
2) insert these value(s) in likesMatch field.
3) remove values found in operation 1 from likesRecieved and likesShown
This is what I am essentially trying to do on the server...
likesRecieved: idA, idB, idE, idF, idL
likesShown: idE, idC, idF
..perform a function to result in the following...
likesRecieved: idA, idB, idL
likesShown: idC,
likesMatch: idE, idF
This is my code to find the ids in both arrays for one document only. The likeMatch helper returns the userIds that may exist in both 'likesRecieved' and 'likesShown' fields within a selected document in Posts collection. The resulting value(s) are then inserting into the likesMatch field.
likeMatch: function() {
var selectedPostId = Session.get('postId'); // _id of document in Post collection
var arrayOfLikeRecieved = Posts.find({_id: selectedPostId}, {fields: {LikesRecieved: 1}}).fetch();
var sumArrayRecieved = _.chain(arrayOfLikeRecieved).pluck('LikesRecieved').flatten().value();
var arrayOfLikeShown = Posts.find({_id: selectedPostId}, {fields: {LikesShown: 1}}).fetch();
var sumArrayShown = _.chain(arrayOfLikeShown).pluck('LikesShown').flatten().value();
var duplicates = _.intersection(sumArrayRecieved, sumArrayShown);
Meteor.call('insertDuplicateIntoMatchField', duplicates);
},
MongoDB doesn't have hooks like some other databases do, so there's no way to automatically have a function called when a document is inserted.
You have a couple of options, though. One way would be to have a hook in your application that runs just before inserting the document to run your function. This could be achieved in meteor by using a Collection.deny function.
If you would prefer to have the function be executed in mongodb, then you'll have to call the function manually. The problem is just how to know when the document was inserted or updated. Luckily, meteor allows you to observe changes to a cursor. You could use that to make a call out to the database and run a stored procedure (function) whenever a document gets updated.