Unable to query using date while passing through mongo data api. I would like to query the database to find fetch all entries at the time
The below code returns empty document
The sample db :
[{
sensorId:'1231541',
sensorTimestamp:'2022-01-28T20:14:26.223+00:00'
}]
await superagent.post(`${url}/action/${body.action}`)
.send({
dataSource: 'Cluster0',
database:'testDB',
collection: body.collection,
filter: body.filter,
sort: body.sort,
});
const config = {
action: 'find',
collection: 'testCollection',
filter: {
sensorId: '1231541',
sensorTimestamp: {
$gte: new Date('2022-01-28T20:14:26.223+00:00'),
$lt: new Date('2022-01-28T20:33:16.324+00:00'),
},
},
sort: {
name: 1,
},
};
You must query dates in millis, not tje string representation of that.
Related
Hello I am trying to populate the data and then trying to paginate that data.
Here is the example
Schema A (Users)
{
name: 'Demo',
postId: 'someObjectId',
}
Schema B (Posts)
{
id: 'someObjectId',
postName: 'Post 1',
date: 'date of creation'
}
Here is my code
const users = UserModel.find({
name: 'Demo'
}).populate({
path: 'postId',
select: 'date',
match: {'postId.date' : {$lt: 'today'}}
}).page(pagination).limit(20)
Not getting the result needed. Can someone point out what's wrong?
NOTE: I have just given the overview. Please don't take it as real code. I know I haven't written what we would write in javascript
A populate have following things:
Post.find({})
.populate([
// here array is for our memory.
// because may need to populate multiple things
{
path: 'user',
select: 'name',
model:'User',
options: {
sort:{ },
skip: 5,
limit : 10
},
match:{
// filter result in case of multiple result in populate
// may not useful in this case
}
}
]);
.exec((err, results)=>{
console.log(err, results)
});
I have a MongoDB collection of documents, using a schema like this:
var schema = new Schema({
name: String,
images: [{
uri: string,
active: Boolean
}]
});
I'd like to get all documents (or filter using some criteria), but retrieve - in the images array - only the items with a specific property (in my case, it's {active: true}).
This is what I do now:
db.people.find( { 'images.active': true } )
But this retrieves only documents with at least one image which is active, which is not what I need.
I know of course I can filter in code after the find is returned, but I do not like wasting memory.
Is there a way I can filter array items in a document using mongoose?
Here is the aggregation you're looking for:
db.collection.aggregate([
{
$match: {}
},
{
$project: {
name: true,
images: {
$filter: {
input: "$images",
as: "images",
cond: {
$eq: [
"$$images.active",
true
]
}
}
}
}
}
])
https://mongoplayground.net/p/t_VxjfiBBMK
I'm trying to do some models in mongoDB using mongoose as a schemaless during working saving data works fine but when i try to fetch the data there is problem
this is the Model Creation code :
const TemplateSchema = new Schema({
any: {},
blog: {
type: Schema.Types.ObjectId,
ref:'blogs'
},
user:{
type: Schema.Types.ObjectId,
ref:'users'
},
date:{
type: Date,
default: Date.now
}
},{ strict: false });
Saving Code :
let newTemplate = {
tempbody: { name: 'Laith', age: 26 },
blog: blog.id,
user: req.session.userid,
}
//console.log(inputObject);
new Template(newTemplate).save()
.then((template) => {
res.redirect(`/blogs/posts/${req.params.blogid}`);
});
Fetching Code :
Template.findOne({_id: req.params.templateid}).populate('blog').then(template =>{//To Fetch ObjectID for Blog from DB
if(template && template.blog.blogID == req.params.blogid) {
console.log(template.tempbody);
res.render('blogs/addPostTemplate', {
template: template,
blogid: req.params.blogid,
templateid: req.params.templateid
});
} else {
res.redirect(`/blogs/posts/${req.params.blogid}`);
}
});
The result suppose to be :
The Object tempbody but it gives always undefined
and if i try to fetch template._id it works fine
and if i print template as full object it also gives the result
and sometimes gives : (node:16840) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
Any help is appreciated.
I've been able to implement a publish method in Meteor that runs a query to my mongo collection through a given attribute when subscribing in the template.js, which works fine but now I'd like to add a multiple attribute search in the same manner. So let's say I have a collection in Mongo where the documents all have the same attributes but with differing values.
{batch:'HAHT020614' color: 'blue', material: 'plastic', printing: true,
model: 'H100', handle: 'plastic', product: 'C010' }
{batch:'HBTH060614' color: 'red', material: 'metal', printing: false,
model: 'V400', handle: 'metal', product: 'P001' }
...
I'm trying to send an object to the publish method which contains all the user chosen fields through reactive vars:
Template.inventory.onCreated( function appBodyOnCreated() {
this.searchQuery = new ReactiveVar({
color: anyItem,
batch: anyItem,
model: anyItem,
material: anyItem,
handle: anyItem,
printing: anyItem,
product: anyItem,
});
this.autorun(function () {
let template = Template.instance();
template.subscribe("stock.search", template.searchQuery.get());
});
});
Then in publication.js:
Meteor.publish('stock.search', function stockQuery(search) {
return Stock.find(
{ $and: [
{color: { $regex : search.color }},
{batch: { $regex : search.batch}},
{product: { $regex : search.product}},
{model: { $regex : search.model}},
{material: { $regex : search.material}},
{handle: { $regex : search.handle}},
{printing: { $regex : search.printing}}
]
},
{ limit: 10, sort: { batch: 1 } });
});
The problem is that some of the search fields will or not be used in the application depending on the user's needs, looking for it to be possible to search all items that are for example both blue and made or metal, and just mix and match whatever is necessary to find.
The object reaches the publish method correctly and I'm able to extract the attributes but the problem resides in the query, as I don't know if it's possible to ask for Mongo to match certain attributes to "any". I've tried to pass on { $exists: true } as a default (and when search field empty) attribute so it matches with any of the documents in the collection but the query doesn't seem to be returning correctly. In this case I'm using regex as some sort of "contains" while the var anyItem is just an empty string.
Is there a proper way to query mongo to match only certain attributes to chosen values while the others stay as "any"?
You could pass only the non-null criteria to the publish method, and build a query with only the given criteria like this:
Meteor.publish('stock.search', function stockQuery(search) {
const criteria = Object.keys(search).map(k => ({ [k]: { $regex: search[k] } }));
return Stock.find(
{ $and: criteria },
{ limit: 10, sort: { batch: 1 } }
);
});
I am trying to import a JSON file into MongoDB inside node. Here is what I am trying to do:
db.open(function(err, db) {
if(!err) {
db.collection('orgs', {safe: true}, function(err, collection) {
if (err) {
console.log("The 'orgs' collection doesn't exist. Creating it with sample data...");
db.collection('orgs', function(err, collection) {
orgs = require("../data/orgs.json");
collection.insert(orgs, {safe:true}, function(err, result) {});
});
}
});
}
});
Note that NodeJS can import JSON automatically as JSON, which is nice, but JSON does not handle objects like ISODate or ObjectID. Here is a snippit of the JSON data I am trying to import:
./data/orgs.json:
{
"stub": "apple",
"name":"Apple, Inc.",
"timezone": "America/New_York",
"currency": "USD",
"plans": [
{"planId":1, "dtStart": {"$date":"2012-12-07 12:34:56"}},
{"planId":0, "dtStart": {"$date":"2012-12-05 23:45:02"}, "dtEnd": {"$date":"2012-12-07 12:34:56"}}
]
}
I am using the mongodb native driver for Node.
I tried to use integer representation of the date, but it did not work.
Is there a way to represent ISODate and ObjectID fields in JSON that MongoDB will recognize?
You can't store dates and other custom objects in JSON, since in supports only Strings, Numbers, Booleans and null's. So, to load them from JSON you shall somehow restore them from their string representation. Of course, you can use BSON instead of JSON since it supports all this data types, but there is a better way to solve your problem.
Since your already using require instead of JSON.parse to load and parse your JSON data, you can make one more step and use JavaScript representation of your data instead of pure JSON:
var ObjectID = require('mongodb').ObjectID;
module.exports = {
stub: "apple",
name: "Apple, Inc.",
timezone: "America/New_York",
currency: "USD",
plans: [
{
planId: new ObjectID("1"),
dtStart: new Date("2012-12-07 12:34:56")
},
{
planId: new ObjectID("0"),
dtStart: new Date("2012-12-05 23:45:02"),
dtEnd: new Date("2012-12-07 12:34:56")
}
]
}
In this case you'll be able to use new Date and new ObjectID to create dates and object ids.
N.B. You can use require('bson').ObjectID instead of require('mongodb').ObjectID, the result will be the same.
We use something like:
import ejson from 'mongodb-extended-json';
import animals from './fixtures/Animals.json';
const array = ejson.deserialize(animals)
where json is like
{ "_id" : { "$oid" : "54ebb..." }, "created" : { "$date" : "2015-02-23T23:19:54.674+0000" }}