Getting just the id of a set of MongoDB documents - mongodb

I have the following problem, I have already done some research in the net and here, but no solution found so far, I would be glad to receive suggestions.
Consider the code:
myModel.create(array).then((justCreated)=>
{
//justCreated is the array of document just created, I can print them out and see!
})
My problem is: I just need their ids, on a array if possible. I could take one by one, but, is there a better way?
I have tried setting the second parameter to "_id" like we do with find or select like we do with populate, but no success. Any suggestion?
PS. I am using mongoose.

According to this post you can see that forEach is much slower than map method. So i think a better solution is the following
myModel.create(array)
.then(justCreated => {
const idsArray = justCreated.map(el => el._id);
})
.catch(err => console.log(err));

After giving some extra thoughts, I have come up with the solution, which I am afraid regarding performace, since my dataset is pretty big, that is the reason I cannot use subdocument, I have tried, but I exceed easily the 16MB maximum limit.
myModel.create(array).then((justCreated)=>
{
justCreated.ForEach((doc)=>{id.push(doc.id))
})
I am opened to suggestion for better ways to handle this problem but this seems to solve the problem, at least at first glance.

Related

Creating a filter in mongo-go-driver for .FindOne

I'm trying to check a collection to see if there is at least one documents that match a specific set of values.
I've tried reading the documentation at https://github.com/mongodb/mongo-go-driver#usage, but I can't seem to find much help there. I'm pretty new to MongoDB & Go, I believe that this is more a problem of my lack of experience.
Here is a sample query from Studio 3T that I'm trying to run with mongo-go-driver:
db.getCollection("events").find(
{
"event.eventType" : "OSR",
"context.vehicleId" : NumberInt(919514),
"ts" : {
"$gte" : ISODate("2019-06-21T21:38:43.022+0000")
}
}
).limit(1);
It seems that the context.FindOne method will do what I want (and eliminating the need for the .limit(1)). I thought that it would be straight forward to "port" this to Go and the mongo-go-driver.
I can sort of make this work, for example I have the following which will find me all the OSR:
var query = &bson.D{
{"event.eventType", "OSR"},
}
result := bson.D{}
e := collection.FindOne(context.TODO(), query).Decode(&result)
This will return me one document. Now, if I want to include the vehicleId value, and I update the query to:
var query = &bson.D{
{"event.eventType", "OSR"},
{"context.vehicleId", 919514},
}
No documents are returned. I haven't bother to expand query to include the ts field yet.
I would expect at to still have at least one document returned, but nothing is showing up. Does anybody have some tips, suggestions or guidance on what I'm doing wrong (or perhaps how I can do this better)?
Not quite sure, but have you tried with bson.M instead of bson.D?
It seems like it's working for me at least.
query := &bson.M{
"event.eventType": "OSR",
"context.vehicleId": 919514,
}
Please refer to the docs for more information.
Also, like #owlwalks said, are you sure, you're in the right collection?

meteor - _id of collection items returning with trailing garbage

I'm returning collection results via a basic find() query and something odd is happening to the {{_id}} I'm receiving. Instead of just the unique id, I'm getting a bunch of trailing garbage that looks like other parts of the document. This is only happening for one collection - the exact same query works perfectly with all my other collections. I'm thinking it may have something to do with how I'm adding collection items, but I'm pretty lost. I can provide more info if needed, but I thought perhaps someone had encountered this before and could advise based on what I've described.
Here's what {{_id}} looks like for documents in the problematic collection:
3D5DWGh9n96BuiC4P""%7B"userId":"hJhLm8iBL9cQDEhzf","limit":10,"skip":0,"props":%7B%7D%7D
Anyone know what's going on here?
NB: Here's extra information I stupidly omitted the first time:
The trailing stuff is not returned when I do I find().fetch(). It's only when I use the easy-search package ( link ) that this happens. Sorry for the lack of clarity earlier.
The helper:
Template.search.helpers({
articlesIndex: () => ArticlesIndex
});
...relevant parts of the template:
{{> EasySearch.Input index=articlesIndex}}
<ul>
{{#EasySearch.Each index=articlesIndex}}
<li>{{title}}</li>
{{/EasySearch.Each}}
</ul>
The hrefs in the search results are where the problematic _ids are.
Actually, it looks like I've figured it out. According to the issues on github, _id is used by the package so I have to refer to __originalId instead. Here's a link in case it helps anyone else: link

ElasticSearch - filter terms for autocomplete

I would like for an autocomplete to get the terms starting with some specific characters.However, the terms returned do not begin with the specified text ("wer" in this case), and more than that, no matter what the prefix content is, they are always the same. The query I am using now is:
{"facets":
{"count":
{"terms":
{"field":"content"},
"facet_filter":
{"prefix":{"content":"wer"}}
}
},
"size":0
}
I am wondering what am I missing or doing wrong. Any help much appreciated. Thanks!
Perhaps you miss a "query" entry after the facet_filter.
If this does not help try regex pattern for facets, but be aware that facets will be removed in future updates of elasticsearch.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-facet.html#_regex_patterns

How to compare 2 mongodb collections?

Im trying to 'compare' all documents between 2 collections, which will return true only and if only all documents inside 2 collections are exactly equal.
I've been searching for the methods on the collection, but couldnt find one that can do this.
I experimented something like these in the mongo shell, but not working as i expected :
db.test1 == db.test2
or
db.test1.to_json() == db.test2.to_json()
Please share your thoughts ! Thank you.
You can try using mongodb eval combined with your custom equals function, something like this.
Your methods don't work because in the first case you are comparing object references, which are not the same. In the second case, there is no guarantee that to_json will generate the same string even for the objects that are the same.
Instead, try something like this:
var compareCollections = function(){
db.test1.find().forEach(function(obj1){
db.test2.find({/*if you know some properties, you can put them here...if don't, leave this empty*/}).forEach(function(obj2){
var equals = function(o1, o2){
// here goes some compare code...modified from the SO link you have in the answer.
};
if(equals(ob1, obj2)){
// Do what you want to do
}
});
});
};
db.eval(compareCollections);
With db.eval you ensure that code will be executed on the database server side, without fetching collections to the client.

How to get whole index in ZEND Lucene?

Hi I am looking for a way to get the whole index if my query is nothing.
My lucene is a picture of my database without some unwanted content, like expired annonces...
So I would like to use only lucene to get annonces, and for that I need a way to get the whole index.
Any ideas?
thanks!
This is not the answer but something wich works for me:
Using an indexKey like is_indexed, always true.
I am adding "is_indexed:1" to my query and it works...
If you have something else, let me know!
I tend to use a field which is named after the index such as:
$oDoc->addField(
Zend_Search_Lucene_Field::keyword(
'index',
'availability'
)
);
Then the term query will return all fields. It's not pretty but it works fine.