In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use ObjectId in Node.js as a query condition, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Insert a piece of data into MongoDB
Insert a piece of data with the following structure in MongoDB:
{_ id: 5d6a32389c825e24106624e4, title: 'are there any interesting projects on GitHub', content: 'last month, a water friend asked me privately if there are any interesting items on GitHub that can be recommended? I said to him, "Yes, I'll clean it up in a couple of days." However, a month later, I forgot all about it and didn't remember 2_05.png until he reminded me yesterday. Creation: 2019-08-31T08:39:20.384Z}
Where the value "5d6a32389c825e24106624e4" of the above _ id is automatically assigned by MongoDB.
Use the ObjectId of MongoDB as the query condition
Note that the value "5d6a32389c825e24106624e4" of _ id is not a string, but an ObjectId object type. Therefore, the following query does not work:
/ / query specified document const findNews = function (db, newsId, callback) {/ / get collection const news = db.collection ('news'); / / query specified document news.findOne ({_ id: newsId}, function (err, result) {if (err) {console.error (' error end:'+ err.stack); return } console.log ("query the specified document, the response result is:"); console.log (result); callback (result);};}
The above newsId needs to be converted to the ObjectId object type. How do I do that? The reference for practice is as follows:
Const ObjectId = require ('mongodb'). ObjectId;// query specified document const findNews = function (db, newsId, callback) {/ / get collection const news = db.collection (' news'); / / query specified document news.findOne ({_ id: ObjectId (newsId)}, function (err, result) {if (err) {console.error ('error end:' + err.stack); return } console.log ("query the specified document, the response result is:"); console.log (result); callback (result);};}
Where require ('mongodb'). ObjectId is used to get the ObjectId class and convert the string newsId to the ObjectId type.
The above is how to use ObjectId as a query condition in Node.js. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.