I need a mongoose query, to select only one record in or condition. There is a collection of blogs. Some of them are in English or in French. Certain blogs are a duplicate like they have the same content but in a different language.But they have same 'group_id'.
If I filter blogs based on some criteria, like 'category' only blogs with the 'userLanguage' should show. But if there is no, show the other language. How do I generate a mongoose query for this?
db.find($and:[{category:'Health'},{$or: [{language: 'en'},{language:'fr'}]}])
But it gives 2 records if both 'en' and 'fr' is present, I need only either 'en' or 'fr'. How can I implement this in one query ?
These are some sample documents,
"{_id: '1', category_id: 'xyz', language:'en', group_id: 'aaa'} ",
"{_id: '2', category_id: 'xyz', language:'fr', group_id: 'aaa'}",
"{_id: '3', category_id: 'xyz' language: 'en', group_id: 'bbb'}",
"{_id: '4', category_id: 'xyz', language: 'fr', group_id: 'ccc'}"
I request for category_id : 'xyz' and language: 'en'. So the result should be,
"{_id: '1', category_id: 'xyz', language:'en', group_id: 'aaa'} ",
"{_id: '3', category_id: 'xyz' language: 'en', group_id: 'bbb'}",
"{_id: '4', category_id: 'xyz', language: 'fr', group_id: 'ccc'}"
I have an array of distinct group_id = ['aaa','bbb','ccc']. So the condition is like,
group_id: $in:group_id $$ language should be 'en' if not 'fr'
Use findOne query, which gives you only one result, like this
db.findOne($and:[{category:'Health'},{$or: [{language: 'en'},{language:'fr'}]}]);
Related
I'm trying to load up all of the emails that have been sent from a transaction via SuiteScript 2.0.
When I run
record.getSublists()
it returns the following:
["item","activeworkflows","workflowhistory","custom8","messages","contacts","activities","usernotes","systemnotes","mediaitem","links","cases","partners","events","calls","tasks"]
However, when I then try to run the following:
record.getSublist('messages');
I receive an error.
I need to be able to check the date of the last email sent so I can determine whether or not to send a follow-up email.
The approach I typically take is to use the Search API to get this type of information:
require(['N/search'], function(search) {
var result = search.create({
type: 'transaction',
filters: [
['internalid', search.Operator.ANYOF, id], 'AND',
['mainline', search.Operator.IS, 'T'], 'AND',
['messages.messagetype', search.Operator.ANYOF, 'EMAIL'], 'AND',
['messages.isincoming', search.Operator.IS, 'F']
],
columns: [
search.createColumn({name: 'internalid', summary: search.Summary.GROUP}),
search.createColumn({name: 'messagedate', join: 'messages', summary: search.Summary.MAX}),
],
}).run().getRange({ start: 0, end: 1 });
var dateOfLastEmail = result[0].getValue({ name: 'messagedate', join: 'messages', summary: search.Summary.MAX });
});
I am relatively new to Node.js, Mongoose and MongoDB.
I want to perform filter functionality and want to filter products by criteria selected by the user.
Is it possible in Node.js to query a response returned by Mongoose?
Sample response from Mongoose as below:
[ { _id: 589860c21f9997fce3502f10,
title: 'Watch',
brand: 'PUMA2',
store: 'ZARA',
for: 'MALE',
size: '32',
colour: 'RED',
userId: '58a420cd7c77aca4b3ce34cd' },
{ _id: 5899bd33c28dbdf2b938f698,
title: 'Watch 2',
brand: 'PUMA',
store: 'ZARA',
for: 'MALE',
size: '32',
colour: 'RED',
userId: '58a420cd7c77aca4b3ce34cd' },
{ _id: 5899bd59c28dbdf2b938f69a,
title: 'Watch 4',
brand: 'PUMA',
store: 'ZARA',
for: 'MALE',
size: '32',
colour: 'RED',
userId: '5899bde3c28dbdf2b938f69e' }]
Now how can I query this response to select data based on brand.
For finding PUMA brand, then query will be
db.collection.find({'brand': 'PUMA'})
Thanks for help everyone, finally got nice plugin called array-query which helped me to achieve what I want.
https://www.npmjs.com/package/array-query
I have the instance where I need to merge 2 tables to one where the result of the second is inserted every nth into the merged table.
to illustrate i have a products table and i need to insert an advert in every nth (say 4th) result of the final output. i have made the assumption the best way to handle these is to keep the two data source tables and merge the desired result into a new table (if theres a better way to use just one table and insert into that I'm interested)
the main data source: products
var productDataTable = [
{id: '1', content_type: 'product', name:'t-shirt'},
{id: '2', content_type: 'product', name:'t-shirt2'},
{id: '3', content_type: 'product', name:'t-shirt3'},
// .....
];
secondary datasource : adverts
var advertsTable = [
{id: 'advert1', content_type: 'advert', name: null},
{id: 'advert2', content_type: 'advert', name: null},
{id: 'advert3', content_type: 'advert', name: null},
// ....
];
desired output of final table required - an advert has been inserted every nth
var feedOutput = [
{id: '1', content_type: 'product', name:'t-shirt'},
{id: '2', content_type: 'product', name:'t-shirt2'},
{id: '3', content_type: 'product', name:'t-shirt3'},
{id: 'advert1', content_type: 'advert', name: null},
{id: '4', content_type: 'product', name:'t-shirt4'},
{id: '5', content_type: 'product', name:'t-shirt5'},
{id: '6', content_type: 'product', name:'t-shirt6'},
{id: 'advert2', content_type: 'advert', name: null},
];
query example
var params = {
TableName: 'feedOutput',
};
docClient.scan(params, function(err, data){
console.log(data)
});
Question:
what would be the most appropriate way to joint these two tables in this nth manner ? i am using node.js with the aws sdk.
thanks in advance.
I have a set of data:
{ name: 'item1', timeOfDay: '36000', dayOfWeek: '1', dayOfMonth: '15', room: '1'}
{ name: 'item2', timeOfDay: '3600', dayOfWeek: '2', dayOfMonth: '10', room: '2'}
{ name: 'item1', timeOfDay: '18000', dayOfWeek: '3', dayOfMonth: '20', room: '3'}
{ name: 'item3', timeOfDay: '72000', dayOfWeek: '4', dayOfMonth: '5', room: '4'}
Given a new item i'm looking for an algorithm to find the closest items order by distance
{ name: 'item2', timeOfDay: '36000', dayOfWeek: '5', dayOfMonth: '3', room: '2'}
First I looked at kMeans to organise items around centers but I have the feeling I need something sorting the item at the good location.
Multicriteria sort ? but that could learn which criteria is the stronger ?
I don't want to do Array.sort() because:
I need to add a new item without sorting all the array
I need to merge same (closest) values
Mongo-DB doesn't support query with wildcards in a nested structure.
In a data-structure that looks like this:
Courses = [{
'name': 'Learning node.js in 1 day',
'roles': {
'team': { subscribed: [ 'User1' ] },
'participant': { subscribed: [ 'User1', 'User2' ] },
'host': { optional: true, subscribed: [] }
}
}]
We would need wildcard-lookup to find subscribers in different roles in order not to use a query like this:
{ $or : [
{"roles.team.subscribed": 'User1'},
{"roles.participant.subscribed": 'User1'}
{"roles.host.subscribed": 'User1'}
]}
Anyway this does not work if we have an open list of roles.
And if we change it to something like this:
'roles': ['team', 'participant', 'host'],
'subscribed': [
{'user':'User1', 'roles': ['team', 'participant']},
{'user':'User2', 'roles': ['participant']}
]
it gets similarly difficult to then find all participants of a course. Either way we have a problem to find all courses a user is subscribed to.
We think about creating a separate collection for subscriptions (back to relational):
{user_id: 'User1', course_id: '456', role: 'participant'}
{user_id: 'User1', course_id: '456', role: 'team'}
{user_id: 'User2', course_id: '456', role: 'participant'}
What is best practice?
We would like to be able to make all different sorts of queries and it seems difficult if it's buried in a nested structure...
Think this is quiet a fundamental question for data-stuctures in mongoDB.
If you don't want to change your structure I think I would do something like this using aggregation framework. In case your fields inside roles are not so many and not completely manageable by users, in that case I would suggest you to use an array in roles and unwind it.
Let's take your first structure:
Courses = [{
'name': 'Learning node.js in 1 day',
'roles': {
'team': { subscribed: [ 'User1' ] },
'participant': { subscribed: [ 'User1', 'User2' ] },
'host': { optional: true, subscribed: [] }
}
}]
Project fields in this way:
name, roles.team, roles.participant, roles.host
So you will end up with this structure
Courses = [{
'name': 'Learning node.js in 1 day',
'roles.team.subscribed': [ 'User1' ] ,
'roles.participant.subscribed': [ 'User1', 'User2' ] ,
'roles.host.subscribed': subscribed: []
}
}]
Then you can unwind subscribed field and get a cartesian product by subscribed, you need to unwind 3 times:
Courses = [
{
'name': 'Learning node.js in 1 day',
'roles.team.subscribed.values': 'User1' ,
'roles.participant.subscribed.values': 'User1',
'roles.host.subscribed.values': ''
}
},
{
'name': 'Learning node.js in 1 day',
'roles.team.subscribed.values': 'User1' ,
'roles.participant.subscribed.values': 'User2',
'roles.host.subscribed.values': ''
}
}
]
At the end you can match every role separately.
Of course you need to use aggregation framework, takes time to get use to it.