I have a Mongodb based Feathers service : Categories and the model is as follows
{
_id : 'SOMEKEY',
categoryName : 'XYZ'
categoryItems : [
{ item_id : 'ITEMID' }
]
}
Now I have After hooks on my Items service that on each Create I want to push the reference of that Item into the categoryItems array in the Categories document. How can I achieve that ? Neither of the built in methods for Category service will help.
Should I be writing Mongoose queries within my hook? If I do that I am depending on Mongoose MongoDB and if my database changes I will have to change all my hooks.
const categoryService = hook.app.service('categories');
categoryService.update(hook.data.item_category,
{
$push: {
"category_items": {
item_id: hook.result._id
}
}
},
{
safe: true, // This value is safely stored in the DB
upsert: true // It creates a new object if
// a previous one does not exist
});
Related
I am trying to select only one field from a mongo document and print the value for it. I found this answer https://stackoverflow.com/a/25589150 which showed how we can achieve this. Below I have tried doing the same yet the entire document ends up getting printed.
const mongoHost =
'somemongourl'
const mongodb = require('mongodb');
const { MongoClient } = mongodb;
MongoClient.connect(
mongoHost,
{ useNewUrlParser: true },
async (error, client) => {
if (error) {
return console.log('Unable to connect to database!');
}
const db = client.db('cartDatabase');
const values = await db
.collection('cart')
.find({ customer_key: 'c_1' }, { customer_key: 1, _id: 0 })
.toArray();
console.log(values);
}
);
This is the output for example I got :-
[
{
_id: new ObjectId("611b7d1a848f7e6daba69014"),
customer_key: 'c_1',
products: [ [Object] ],
coupon: '',
discount: 0,
vat: 0,
cart_total: 999.5,
cart_subtotal: 999.5
}
]
This is what I was expecting -
[
{
customer_key: 'c_1'
}
]
The standard Node.js MongoDB driver requires a top-level projection property for the options parameter if you wish to project your documents. This would result in the second parameter of your find() call looking like this:
{ projection: { customer_key: 1, _id: 0 } }
This is indicated in the Node.js MongoDB driver API documentation, which is notably not a 1-to-1 match with the MongoDB shell API.
As of the time of this answer, you could find the collection.find() reference here. This reference shows the following method signature (again as of when this answer was written):
find(filter: Filter<WithId<TSchema>>, options?: FindOptions<Document>)
Following the FindOptions parameter takes us to this reference page, which details the various top-level options properties available for the find() method. Among these is the projection property in question.
In short, don't use the normal MongoDB documentation as a reference for your programming language's MongoDB driver API. There will often be disconnects between the two.
I have MongoDB database (with Mongoose) containing a collection of Products (among others), which looks like this:
[
{
name: 'Product A',
url: 'product-a',
category: 'accesory',
price: 12,
shortDescription: ['example description'],
technicalSpecs: [{ speed: 10, weight: 20 }],
images: [],
reviews: [],
relatedProducts: [
{
url: 'product-b',
name: 'Product B',
// to be added in Update query
//id: id_of_related_product
}
]
} /* other Product objects */
]
As every MongoDB document is provided with _id property by default, but within the relatedProducts array i only have url and name properties, i want to add the id property (associated with corresponding Product) for each object in the relatedProducts array, so i will be able to conveniently query and process those related products.
I came up with an idea to query all Products to get only those, which have non-empty relatedProducts array. Then i loop them and i search for Product model, which has specific url and name properties - this let's me get it's true (added by MongoDB) _id. At the end i want to add this _id to matching object inside relatedProducts array.
My code:
async function assignIDsToRelatedProducts(/* Model constructor */ Product) {
const productsWithRelatedOnes = await Product.find(
{ relatedProducts: { $ne: [] }}, ['relatedProducts', 'name', 'url']
);
for (const productItem of productsWithRelatedOnes) {
for (const relatedProduct of productItem.relatedProducts) {
const product = await Product.findOne(
{ url: relatedProduct.url, name: relatedProduct.name },
'_id'
);
// throws error
await productItem.updateOne(
{ 'relatedProducts.url': relatedProduct.url },
{ $set: { 'relatedProducts.$.id': product._id } }
);
}
}
}
However it throws the following error:
MongoError: Cannot create field 'url' in element {relatedProducts: [ /* array's objects here */ ]}
I don't know why MongoDB tries to create field 'url', as i use it to project/query url field (not create it) in updateOne method. How to fix this?
And - as i am newbie to MongoDB - is there a simpler way of achieving my goal? I feel that those two nested for..of loops are unnecessary, or even preceding creation of productsWithRelatedOnes variable is.
Is it possible to do with Mongoose Virtuals? I have tried it, but i couldn't match virtual property within the same Product Model - attach it to each object in relatedProducts array - after calling .execPopulate i received either an empty array or undefined (i am aware i should post at-the-time code of using Virtual, but for now i switched to above solution).
Although i didn't find solution or even reason of my problem, i solved it with a slightly other approach:
async function assignIDsToRelatedProducts(Product) {
const productsHavingRelatedProducts = Product.find({ relatedProducts: { $ne: [] }});
for await (const withRelated of productsHavingRelatedProducts) {
for (const relatedProductToUpdate of withRelated.relatedProducts) {
const relatedProduct = await Product
.findOne(
{ url: relatedProductToUpdate.url, name: relatedProductToUpdate.name },
['url', '_id']
);
await Product.updateMany(
{ 'relatedProducts.url': relatedProduct.url },
{ $set: { 'relatedProducts.$.id': relatedProduct._id } }
);
}
}
const amountOfAllProducts = await Product.find({}).countDocuments();
const amountOfRelatedProductsWithID = await Product
.find({ 'relatedProducts.id': { $exists: true } }).countDocuments();
console.log('All done?', amountOfAllProducts === amountOfRelatedProductsWithID);
}
Yet, i still suppose it can be done more concisely, without the initial looping. Hopefully somebody will suggest better solution. :)
So I am currently working on a project with mongodb and nodejs and I was wondering, how can you update data in mongodb via nodejs? My problem is that I want to keep the old data and add new. For example, here is the data currently in my mongodb
{
"_id" : ObjectId("5a1c0c1c3b147ec2e31cceb3"),
"event_id" : "1",
"event_medium" : "null",
"event_tags" : ["#JustTesting"]
}
So I want to add new data to the event_tags array and still keep the old data.
So for example the end result would be this:
{
"_id" : ObjectId("5a1c0c1c3b147ec2e31cceb3"),
"event_id" : "1",
"event_medium" : "null",
"event_tags" : ["#JustTesting", "#Test", "#Something"]
}
You should use the update function of MongoDB for that. MongoDB knows different update operators, in your case you may use $push or $pushAll (the second is deprecated):
update one after the other with $push
YourCollection.update({ _id: 'xxx' }, { $push: { event_tags: '#Test' } });
YourCollection.update({ _id: 'xxx' }, { $push: { event_tags: '#Something' } });
or both at once with $pushAll (deprecated now)
YourCollection.update({ _id: 'xxx' }, { $pushAll: { event_tags: ['#Test', '#Something'] } });
To interact with MongoDB form your NodeJS app, I would use a library like this one.
Your starting point is the Update function in CRUD (Create,Read, Update, Delete) operations in Mongodb.
Your node program should have among others the update function where you set the _id field you want to update and load the content fields to be update in 'data' for example as below:
myModel.prototype.update = function (_id, data, callback) {
const query = { _id: this.mongo.ObjectId(_id) };
debug(' update:' + JSON.stringify(query));
this.mongo.collection('mycollection').update(query, data, callback);
};
This piece of code should be put in your Model, if you use MVC pattern.
There is a lot to go.
Honestly I recommend a more deep tutorial like parts 3 and 4 of this one for nodejs and Mongoose (mongo db driver):
MDN tutorial for mongo/node/express
I assume you are using mongoose..
eventModel.findOne({event_id:1 },
function(err, eventObj){
if(err){
//handle error
} else {
if(eventObj === null) {
//event doesnot exist
}
var tagList = eventObj.event_tags;
tagList.push('new_tag1');
tagList.push('new_tag2');
eventObj.event_tags = tagList;
eventObj.save(function(err){
if(err){
//handle error
} else {
//success
}
})
I want to update a value of a sub-document where a message has a specific id and an user id is in a recipient array. I want to update a value of the matched object with the specified user id.
When I run the following query on the MongoDB CLI, everything works and the value is updated:
db.getCollection('messages').update({
_id : ObjectId("57d7edb8c497a75a6a7fde60"),
"recipients.userId" : "5789127ae2bcc79326462dbc"
},{
$set : {"recipients.$.read": true}
});
But when I run the following query via JS in my FeathersJS application:
messageService.update({
_id : '57d7edb8c497a75a6a7fde60',
"recipients.userId" : "5789127ae2bcc79326462dbc"
},{
$set: {"recipients.$.read": true}
}).then(function(e) {
console.log(e)
}).catch(function(e) {
console.log(e);
});
I get the error:
GeneralError: The positional operator did not find the match needed from the query. Unexpanded update: recipients.$.read
What am I doing wrong?
And is there a better way of updating many messages at once?
Thanks!
For updating one or multiple records by a query you need to call the service method by setting id to null and putting the query into params.query:
messageService.update(null, {
$set: {"recipients.$.read": true}
}, {
query: {
_id : '57d7edb8c497a75a6a7fde60',
"recipients.userId" : "5789127ae2bcc79326462dbc"
}
}).then(function(e) {
console.log(e)
}).catch(function(e) {
console.log(e);
});
I have two models - User and Tag. Part of User model
module.exports = {
attributes: {
tags: {
collection: "tag",
via: "users"
}
}};
Tag model looks like
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
users: {
collection: "user",
via: "tags"
},
}};
And I need to sort tags by "users.length" (each tag can has different number of users). I tried this
Tag.find().populate('users').sort('users.length DESC').exec(function(err, tags){});
And also I tried something like this
Tag.native(function (err, collection) {
if (err) return res.serverError(err);
collection.aggregate(
[
{ $sort : { users : -1 } }
], function (err, results) {
});
});
But I had a fail. Documents are not sorted in right order. Can you help me to sort documents as I want please? I'm a new in Sails and in Mongo too. And I have no idea how I can do this.
P.S.
I use sails-mongo adapter version 0.11.2
Unfortunately, there's no way of doing this as of now.
I would suggest maintaining a count field in the Tag model itself and updating it each time a User adds or removes a Tag. Then run a normal query like you did with count as the sort key. Doing this also allows you to query faster if you just need Tag information (and not the users) since you can skip the populate() call which internally references another collection.